zhifu ge - how to be weird in ruby - with notes
TRANSCRIPT
https://www.youtube.com/watch?v=5MgBikgcWnY
It takes 20 hours of deliberate practice to be reasonably good at something, anything.
The Algorithm of Rapid Ruby Acquisition
1. Learn enough to self-correct
2. Practice at least 20 hours
Practice at least 20 hourscodeeval.com
The second step is easy. There are numerous online resources for you to practice. One example of them is codeeval.com
Learn enough to self-correctlisten to this talk
The first step, however, is easier. You only have to listen to this talk. That’s recursion right there
a, b, c, d = 'foo', 'bar', 'baz', 'foobar'
this:but just because you can doesn’t mean you should: readability is bad
examples• String#empty?
• String#include?
• String#start_with?
• String#end_with?
• Array#empty?
• Array#include?
• Range#include?
• Object#respond_to?
• Object#nil?
• Object#is_a?
Obejct#is_a? synonymous kind_of?/instance_of?
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
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
if(condition1) { result = x;} else if(condition2) { result = y;} else { result = z;}
In Java
if condition1: result = xelif condition2: result = yelse: result = z
In Python
if !condition do_somethingend
negative condition unless
unless condition do_somethingend
so this is not good enough
do_something if !condition
negative condition unless
do_something unless condition
this isQuestion: which one is better? Choose unless over if with negative condition
while(true) { doSomething(); if(condition1) { break; }
if(condition2) { continue;}
}
In Java
while True: do_something() if condition1: break if condition2: continue
In Python
loop is a method, not a keyword
loop do do_something break if condition1 next if condition2end
Kernel#loop repeatedly executes the block.We call this kind of ruby methods iterators. They take a block, and execute the block repeatedly.
In Ruby
s = “hello"
s.each_char do |char| puts charend
demoalso demo: how to print index of each char
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.
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.
In Ruby
a = [1, 2, 3]
a = a.map { |i| i + 1 }
DemoArray#collect = Array#mapInvokes the given block once for each element of self.change every int to string
In Ruby
a = [1, 2, 3]
sum = a.inject { |a, e| a + e }
DemoEnumerable#reduce = Enumerable#injectshortcut: inject(:+)
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 endend
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,
def greet puts “hello” “hello”end
what is returned?
def greet “hello” puts “hello”end
def greet return “hello” puts “hello”end
Ruby Style Guidehttps://github.com/bbatsov/ruby-style-guide