@suggestions   @rss   @archive   @codeforpeople.com     @random   @radio[:m3u|:pls|:ruby]   @family   @neighbors  @twitter 



Ruby: Learn something every day…

Possibly I’m the most experienced Rubyist to ever discover this, but nonetheless it came as a suprise to me:

  class C; end

  c = C.new

  c.instance_eval def foo() 42 end

  c.instance_eval do
    def bar() 42.0 end
  end

  p c.foo #=> 42
  p c.bar #=> 42.0

I have been in the habit of using the singleton class for such tricks in the past, but this is a nice shortcut.

Comments (View)
blog comments powered by Disqus