Read more

Literally 101 Ruby tricks

Dominik Schöler
October 11, 2012Software engineer at makandra GmbH

The linked slidedeck holds many tips, of which I list the most interesting to me below

DATA and END

Illustration book lover

Growing Rails Applications in Practice

Check out our e-book. Learn to structure large Ruby on Rails codebases with the tools you already know and love.

  • Introduce design conventions for controllers and user-facing models
  • Create a system for growth
  • Build applications to last
Read more Show archive.org snapshot

The __END__ keyword tells Ruby where a file ends – but you don't have to stop there. Any text you add after it is accessible via the DATA object.

Running the attached example file data.rb prints:

DATA is a File object to everything in the current file after "__END__"

No braces needed for 'special variables'

@instance, @@class, $global = [ 'instance', 'class', 'global' ]
puts "#@instance, #@@class, #$global"

Infinite arrays

ring = [1, 2, 3].cycle
p ring.take(5)
# => [1, 2, 3, 1, 2]

I haven't made it beyond trick #44, it was just too much for now.
But you, go and add your favourites!

Posted by Dominik Schöler to makandra dev (2012-10-11 21:01)