From: "Peña, Botp" Date: 2004-10-12T12:42:24+09:00 Subject: Re: [Q] case based on object class Robert Klemme [mailto:bob.news@gmx.net] wrote: > The OO way is to use a mapping IMHO: > > ACTION_MAPPING = { > Fixnum => lambda { puts "Fixnum" }, > Float => lambda { puts "Float" }, > String => lambda { puts "String" }, > } > > def myfunc(klass) > code = ACTION_MAPPING[klass] > raise ArgumentError, "Klass not found: #{klass}" unless code > code.call > end > > You can even define a block for the error as default > effectively rendering the method a one liner like this: > > ACTION_MAPPING = Hash.new( lambda { raise ArgumentError, > "Klass not found" } ).update( > Fixnum => lambda { puts "Fixnum" }, > Float => lambda { puts "Float" }, > String => lambda { puts "String" } > ) > > def myfunc(klass) ACTION_MAPPING[klass].call end hehe. I love posting of robert. very oo. c-oo-l. thanks robert. > > Kind regards > > robert kind regards -botp