ruby pearls

Dec 07, 2016 14:20


2.2.0 :004 > ':xyz'.split(/:/)[0]
=> ""
2.2.0 :005 > ''.split(/:/)[0]
=> nil
2.2.0 :006 > 'xyz'.split(/:/)[0]
=> "xyz"

2.2.0 :001 > weak_key = lambda {|c| c.split(/:/, 2)[0]}
2.2.0 :002 > weak_key('xyz:1986')
NoMethodError: undefined method `weak_key' for main:Object

2.2.0 :003 > def weak_key(x)
2.2.0 :004?> puts "blah"
2.2.0 :005?> end
=> :weak_key
2.2.0 :006 > def xyz(x)
2.2.0 :007?> def weak_key(y)
2.2.0 :008?> puts x+y
2.2.0 :009?> end
2.2.0 :010?> puts "yo"
2.2.0 :011?> end
=> :xyz
2.2.0 :012 > weak_key('b')
blah
=> nil
2.2.0 :013 > xyz('b')
yo
=> nil
2.2.0 :014 > weak_key('c')
NameError: undefined local variable or method `x' for main:Object
from (irb):8:in `weak_key'
from (irb):14
from /Users/alex/.rvm/rubies/ruby-2.2.0/bin/irb:11:in `'

Idiots.
Previous post Next post
Up