From: furtive.clown@... Date: 2007-11-13T18:45:02+09:00 Subject: Re: Whats the ruby way of associating metadata with a function On Nov 13, 3:43 am, Keith Chapman wrote: > > In javascript I'm used to associating metadata with a function as > follows. > > foo.bar = "foobar"; > function foo(){ > > } > > Whats the normal ruby practice to do this kind of a thing. Whats the > ruby way of doing this. Three options come to mind: ================================================== #1 : add accessors to the singleton of a lambda foo = lambda { puts "foo!!" } class << foo attr_accessor :bar end foo.bar = "foobar" foo.call # => "foo!!" puts foo.bar # => "foobar" ================================================== #2 : add a hash to the singleton of a lambda foo = lambda { puts "foo!!" } class << foo def meta @meta ||= Hash.new end end foo.meta[:bar] = "foobar" foo.call # => "foo!!" puts foo.meta[:bar] # => "foobar" ================================================== #3 : add a method to the singleton of an OpenStruct require 'ostruct' foo = OpenStruct.new class << foo def call puts "foo!!" end end foo.bar = "foobar" foo.call # => "foo!!" puts foo.bar # => "foobar" All other things being equal, I would be inclined toward #3.