0x10 – Merry Christmas and Happy Contemplating…

…some piece of ruby code that might not behave as you think (depending on your thinking, of course.). Do you know the results?


Note: I finally managed to find the option that allows you to comment on my blog without being logged into wordpress.com. Feel free to comment your findings below.

What is the output of the following?

puts false or 1
puts false || 1
puts false | 1

What is the output of the following?

puts 1 and false
puts 1 && false

What is the result of

puts File.dirname("foo.bar")
puts File.dirname("/foo.bar")
puts File.dirname("/foo.bar/")

How many times…

this script file wishes you a merry X-Mas when run via ‘ruby <filename>‘?

puts "Merry XMas!"
require __FILE__
require File.dirname(__FILE__) + "/" + File.basename(__FILE__)

And how many times…

…with this script?

puts "Merry XMas!"
require __FILE__
require File.dirname(__FILE__) + "/" + File.basename(__FILE__)

Dir.mkdir "a" rescue nil
require File.dirname(__FILE__) + "/a/../" + File.basename(__FILE__)

And when running from…

…within a Rails application via ‘ruby script/runner‘?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s