Ruby: Syntax and Terminology Flashcards
When calling a method, the object before the period is called?
The receiver
Using one line statement modifiers
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
@isbn = isbn
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.
Ruby block
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.
Defining Local Block Variables
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
Parallel Assignment Syntax
i1, i2 = 4, 1 # parallel assignment (i1 = 4 and i2 = 1)
Special Syntax for inject method
[1,3,5,7].inject(:+) # => 16
[1,3,5,7].inject(:*) # => 105
5 Ways to Construct String Literals
- ’ ‘
- ” “
- %q - thin quote, same as #1
- %Q - thick quote, same as #2
- here document
In fact, the q/Q is optional.
%{Seconds/day: #{246060}}
=> Seconds/day: 86400
What is a here document?
string = «_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
What are methods that start with a capital letter used for?
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 do you specify default values for a method’s arguments?
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.”
What is splatting and argument?
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
return statement
[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.
Example of Using Lamba / proc
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.
Swap the values in two variables
a, b = b, a