Contenu connexe Similaire à Ruby for newbies Lake Ainsworth (19) Plus de Andrew Grimm (20) Ruby for newbies Lake Ainsworth3. Just about everything is an Object
>> 42.is_a?(Object)
=> true
>> "hello world".is_a?(Object)
=> true
>> nil.is_a?(Object)
=> true
>> Object.is_a?(Object)
=> true
>> Class.is_a?(Object)
=> true
4. Just about all instructions are
methods
puts “hello world” is a call to the method `puts`
>> private_methods.grep(/puts/)
=> ["puts"]
6. Ruby (kind of) does functional
programming
grouping_by = Proc.new {|x| x % 6 }
what_to_do = Proc.new {|x| p x}
numbers = 1..50
def group_by_and_process(numbers, grouping_by,
what_to_do)
what_to_do.call(numbers.group_by(&grouping_by))
end
8. You can do just about anything
old_stdout = STDOUT
STDOUT = StringIO.new
STDOUT.puts "this won't be printed"
STDOUT = old_stdout
STDOUT.puts "this will be printed"