This document discusses ways that Ruby code can be "weird" compared to other languages. It covers 11 weirdnesses:
1. Parallel variable assignment
2. Method names ending with ? for booleans
3. Everything having a boolean value
4. if/unless statements returning values
5. The unless keyword for negative conditions
6. Iterators like loop and times for loops
7. Iterators for arrays and strings
8. Optional parentheses for method calls
9. Methods being able to take blocks
10. Implicit return of the last expression in a method
11. Resources for learning more about Ruby idioms
3. The Algorithm of
Rapid Ruby Acquisition
1. Learn enough to self-correct
2. Practice at least 20 hours
4. Practice at least 20 hours
codeeval.com
The second step is easy. There are numerous online resources for you to practice. One example of them is codeeval.com
5. Learn enough to self-correct
listen to this talk
The first step, however, is easier. You only have to listen to this talk.
That’s recursion right there
6. Learn enough to self-correct
• Assignment
• Naming
• Control Expressions
• Methods
18. Weirdness #3
everything has a Boolean value
Every expression in Ruby evaluates to an object, and every object has a Boolean value of either true or false? true and false are objects
19. if 0
puts “0 is true”
else
puts “0 is false”
end
what is the output?
20. Only two objects has a false
value, nil and false itself
Every expression in Ruby evaluates to an object, and every object has a Boolean value of either true or false? true and false are objects
22. if(condition1) {
result = x;
} else if(condition2) {
result = y;
} else {
result = z;
}
In Java
if condition1:
result = x
elif condition2:
result = y
else:
result = z
In Python
35. do_something if !condition
negative condition unless
do_something unless condition
this is
Question: which one is better? Choose unless over if with negative condition
40. loop is a method, not a keyword
loop do
do_something
break if condition1
next if condition2
end
Kernel#loop repeatedly executes the block.
We call this kind of ruby methods iterators. They take a block, and execute the block repeatedly.
41. how to do something a certain
number of times?
iterators help you iterate
42. for(int i = 0; i < 10; i++) {
doSomething();
}
In Java
for i in range(10):
do_something()
In Python
58. puts “hello” puts(“hello”)
Omit parentheses when:
Omit parentheses around parameters for methods that are part of an internal DSL (e.g. Rake, Rails, RSpec),
methods that have "keyword" status in Ruby (e.g. attr_reader, puts) and attribute access methods. Use parentheses around the arguments of all other method
invocations.
59. omit parentheses when:
• there are no arguments
• the method is part of internal DSL
• the method has “keyword status”
attr_reader, puts, print
Omit parentheses when:
Omit parentheses around parameters for methods that are part of an internal DSL (e.g. Rake, Rails, RSpec),
methods that have "keyword" status in Ruby (e.g. attr_reader, puts) and attribute access methods. Use parentheses around the arguments of all other method
invocations.
62. In Java
int[] a = {1, 2, 3};
for(int i = 0; i < a.length; i++) {
a[i]++;
}
63. In Python
a = [1, 2, 3]
for i in range(len(a)):
a[i] += 1
64. In Ruby
a = [1, 2, 3]
a = a.map { |i| i + 1 }
Demo
Array#collect = Array#map
Invokes the given block once for each element of self.
change every int to string
70. class SomeControllerTest < ActionController::TestCase
test “should do stuff” do
assert_equal 2, 1 + 1
end
def test_should_do_stuff
assert_equal 2, 1 + 1
end
end
72. the evaluated object of the last
executed expression in a method
is implicitly returned to the caller
Every expression in Ruby evaluates to an object,