Ruby: Syntax and Terminology Flashcards

1
Q

When calling a method, the object before the period is called?

A

The receiver

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

Using one line statement modifiers

A

Example 1:
Original:

if radiation > 3000
puts “Danger, Will Robinson”
end

One-Line Statement Modifier:

puts “Danger, Will Robinson” if radiation > 3000

Example 2:
Original:

square = 2
while square < 1000
square = square*square
end

One-Line Statement Modifier:

square = 2
square = square*square while square < 1000
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

@isbn = isbn

A

It’s easy to imagine that the two variables here, @isbn and isbn, are somehow related—it looks like they have the same name. But they don’t. The former is an instance variable, and the “at” sign is actually part of its name.

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

Ruby block

A

A block is simply a chunk of code enclosed between either braces or the keywords do and end.

Style:

All things being equal, the current Ruby style seems to favor using braces for blocks that fit on one line and do/end when a block spans multiple lines.

Blocks can appear in Ruby source code only immediately after the invocation of some method. If the method takes parameters, the block appears after these.

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

Defining Local Block Variables

A

You can now define block local variables by putting them after a semicolon in the block’s parameter list.

Example:

square = "some shape"
sum = 0
[1, 2, 3, 4].each do |value; square|
square = value * value
      sum   += square
    end
    puts sum
    puts square
produces:
30
some shape
# this is a different variable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Parallel Assignment Syntax

A
i1, i2 = 4, 1
# parallel assignment (i1 = 4 and i2 = 1)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Special Syntax for inject method

A

[1,3,5,7].inject(:+) # => 16

[1,3,5,7].inject(:*) # => 105

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

5 Ways to Construct String Literals

A
  1. ’ ‘
  2. ” “
  3. %q - thin quote, same as #1
  4. %Q - thick quote, same as #2
  5. here document

In fact, the q/Q is optional.

%{Seconds/day: #{246060}}

=> Seconds/day: 86400

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

What is a here document?

A

string = &laquo_space;< characters. Normally, this terminator must start in the first column. However, if you put a minus sign after the < < characters, you can indent the terminator.

Example with minus sign:

string = < END_OF_STRING

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

What are methods that start with a capital letter used for?

A

By convention, methods names starting with an uppercase letter are used for type conversion. The Integer method, for example, converts its parameter to an integer.

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

How do you specify default values for a method’s arguments?

A

You do this using an equals sign (=) followed by a Ruby expression.

Example:

def cool_dude(arg1="Miles", arg2="Coltrane", arg3="Roach") "#{arg1}, #{arg2}, #{arg3}."
end

cool_dude # => “Miles, Coltrane, Roach.”

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

What is splatting and argument?

A

Splatting an argument is placing an asterisk before the name of the parameter after the “normal” parameters, that lets you pass in a variable number or arguments.

def varargs(arg1, *rest)
"arg1=#{arg1}. rest=#{rest.inspect}"
end

When you use a splat, all of the arguments for the ‘splat’ are bundled into an array. The array is assigned to the parameter.

You can put the splat argument anywhere in a method’s parameter list, allowing you to write this:

def split_apart(first, *splat, last)
puts "First: #{first.inspect}, splat: #{splat.inspect}, " +
"last: #{last.inspect}"
    end
    split_apart(1,2)
split_apart(1,2,3) split_apart(1,2,3,4)

produces:

First: 1, splat: [], last: 2
First: 1, splat: [2], last: 3
First: 1, splat: [2, 3], last: 4

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

return statement

A

[for methods] Ruby has a return statement, which exits from the currently executing method. The value of a return is the value of its argument(s). It is idiomatic Ruby to omit the return if it isn’t needed.

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

Example of Using Lamba / proc

A

A proc [ my own def ] is used to pull out duplicate code that may be repeated in the middle of a block. For example:

print "(t)imes or (p)lus: " operator = gets
print "number: "
number = Integer(gets)
if operator =~ /^t/
puts((1..10).collect {|n| n*number }.join(", "))
else
puts((1..10).collect {|n| n+number }.join(", "))
end
produces:
(t)imes or (p)lus: t
number: 2
2, 4, 6, 8, 10, 12, 14, 16, 18, 20

This method duplicates the range and the join. So, we could pull them out, put them in a proc. The result is this.

print "(t)imes or (p)lus: " operator = gets
print "number: "
number = Integer(gets)
if operator =~ /^t/
calc = lambda {|n| n*number }
else
calc = lambda {|n| n+number }
end puts((1..10).collect(&calc).join(", "))
produces:
(t)imes or (p)lus: t
number: 2
2, 4, 6, 8, 10, 12, 14, 16, 18, 20

If the last argument is preceded by an ampersand, Ruby assumes that is a Proc object. Removes it from the parameter list, converts the Proc into a block, and associates it with the method.

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

Swap the values in two variables

A

a, b = b, a

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

||=

A

A common idiom is to use ||= to assign a value to a variable only if that variable isn’t already set:

var ||= “default value”

This is almost, but not quite, the same as var = var || “default value”. It differs in that no assignment is made at all if the variable is already set. In pseudocode, this might be written as var = “default value” unless var or as var || var = “default value”.

17
Q

What are the 10 comparison methods of Ruby?

A

==

===

=~

eql?

equal?

>

> =

<

<=

18
Q

then keyword for if statements, when do you use?

A

If you want to lay out your code more tightly, you must separate the boolean expression from the following statements with the then keyword:

if artist == “Gillespie” then handle = “Dizzy”
elsif artist == “Parker” then handle = “Bird”
else handle = “unknown”
end

19
Q

Understand break, next

A

break terminates the immediately enclosing loop; control resumes at the statement following the block.

next skips to the end of the loop, effectively starting the next iteration

These keywords can also be used within blocks. Although you can use them with any block, they typically make the most sense when the block is being used for iteration

i=0 loop do
      i += 1
      next if i < 3
      print i
      break if i > 4
end
produces:
345
20
Q

Variable Scope during Iterators and Blocks

A

The while, until, and for loops are built into the language and do not introduce new scope; previously existing locals can be used in the loop, and any new locals created will be avail- able afterward.

The blocks used by iterators (such as loop and each) are a little different. Normally, the local variables created in these blocks are not accessible outside the block:

21
Q

Declaring Block Locals

A

Example 1 - no block locals.

square = "yes"
total = 0
[ 1, 2, 3 ].each do |val|
      square = val * val
      total += square
    end
puts "Total = #{total}" puts "Square = #{square}"

produces:
Total = 14
Square = 9

Example 2 - block locals (using ;)

square = "yes"
total = 0
[ 1, 2, 3 ].each do |val; square|
      square = val * val
      total += square
    end
puts "Total = #{total}" puts "Square = #{square}"
produces:
Total = 14 
Square = yes