Keyword Precedence

The Ruby keyword not and the unary ! aren’t the same. They have a different precedence, and therefore are not interchangeable, despite seeming very similar. irb > not 3 == 4 => true irb > !3 == 4 => false The first example is evaluated as not (3 == 4), or not false, which is true. The second example is evaluated as (not 3) == 4, or false == 4, which is false. ...

July 2, 2015 · 1 min · Jake Worth

Skid Keywords

Ruby has a few interesting old keywords that use the underscore, or ‘skid’, to convey meta information about the file. Here they are in action: # test.rb puts __ENCODING__ puts __FILE__ puts __LINE__ __END__ Because this is after 'end', it should not be executed Running this file produces: $ ruby test.rb UTF-8 test.rb 3 Pretty cool!

July 2, 2015 · 1 min · Jake Worth

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

May 26, 2015 · 1 min · Jake Worth

Truthy Strings

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

May 26, 2015 · 1 min · Jake Worth

Minmax

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"]

May 18, 2015 · 1 min · Jake Worth

Case Insensitive Matchers

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.

May 15, 2015 · 1 min · Jake Worth

Ruby Symbol#to_proc

What does &: mean in Ruby? In this post, I’ll explain. ...

October 31, 2014 · 2 min · Jake Worth

Code Club: Rack and Rails Server

This week in Code Club we explored Rack, the middleware of the Rails stack. Rack is newsworthy of late due to a change in ownership, prompting inevitable questions about the future. As Rails developers, we wanted to know what Rack really does. ...

August 27, 2014 · 3 min · Jake Worth

Don’t miss my next essay

Hear from me immediately when I post: no ads, unsubscribe anytime.