Puts Multiple Lines
Ruby’s puts can take multiple arguments! 2.2.0 :002 > puts 'so', 'many', 'statements', 'to', 'puts' so many statements to puts => nil
Ruby’s puts can take multiple arguments! 2.2.0 :002 > puts 'so', 'many', 'statements', 'to', 'puts' so many statements to puts => nil
Ruby strings are truthy, and they evaluate to zero, unless they start with a number. > 'nine to five'.to_i => 0 > '9 to 5'.to_i => 9
Ruby’s Enumerable class includes minmax, which returns a two element array with the minimum and maximum values of an enumerable. > [1, 5, 10].minmax => [1, 10] > ['alpha', 'bravo', 'zulu'].minmax => ["alpha", "zulu"]
You can make Ruby regex matchers case insensitive with an i. 2.1.0 :001 > 'Expected output' =~ /expected/ => nil 2.1.0 :002 > 'Expected output' =~ /expected/i => 0 This is useful when writing tests, when you care about a message, but are not interested in its exact format.
What does &: mean in Ruby? In this post, I’ll explain. Explore a Rails app, and you might see something like this: def self.names find(:all).collect(&:name) end What is that &:, you might be wondering? It’s shorthand for this: def self.names find(:all).collect { |p| p.name } end Often in Ruby, we are using a method that invokes a given block. The methods map, collect, and select are just three examples. To employ one of these methods, you might write something like this: ...