Ruby Fundamentals Flashcards

The basics of the Ruby language

1
Q

Ruby Basic Data Types

A

Numeric (Float or integer)
Boolean
String

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Ruby Math Operators

A
  • Addition: +
  • Subtraction: -
  • Multiplication: *
  • Division: /
  • Exponentiation: **
  • Modulo: %
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Ruby Assignment Operators

A

Normal Assignment:
i = <value></value>

Conditional Assignment:
i ||= <value>
(only assigns new value if i = nil)</value>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Numeric Modification Operators

A

Increment:
i += value
Decrement:
i -= value
Multiply:
i *= value
Divide:
i /= value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Output Methods

A

print “string”
prints string or value to screen

puts “string”
prints string or value and adds newline

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Input Method

A

variable = gets

retrieves user input as a string

Note: adds a newline character which may need to be removed with String.chomp method:

variable = gets.chomp

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Ruby File Extension

A

.rb

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How is whitespace interpreted in Ruby

A

Whitespace is generally ignored.

Exceptions:
* White space in strings
* after method names, may treat the next value as a parameter being passed in

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Ending statements or lines in Ruby

A

newline character and semicolon are both interpreted as the end of a current statement/line:

print “hello”
print “hello”;

Exception: lines ending with operator expect values in the next line:

print “hello “ +
“world”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

String method to remove newline from end

A

.chomp

Example:
“Hello World\n”.chomp

returns “Hello World”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Calling Object Methods

A

Syntax:

object.method

or

object.method(parameter)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Modify calling object with method

A

Use the ! operator at the end of the method name:

object.method!

The object will be modified instead of a new object being returned

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Single Line comment

A

Use the ‘#’ operator:

#this is a comment

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Multiple Line Comments

A

=begin
this is
my comment
=end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly