Skip to content

Instantly share code, notes, and snippets.

@JonathonMA
Created August 28, 2013 01:21
Show Gist options
  • Select an option

  • Save JonathonMA/6361079 to your computer and use it in GitHub Desktop.

Select an option

Save JonathonMA/6361079 to your computer and use it in GitHub Desktop.
Classy
class Symbol
def with(arg)
lambda { |obj| obj.send(self, arg) }
end
end
tweedledee = Class.new.new
tweedledum = Object.new
heroes = [tweedledum, tweedledee]
yell = :upcase.to_proc # == lambda { |str| str.upcase }
whisper = lambda { |str| str.gsub /./, '.' }
p tweedledee.class # => #<Class:0x9081110>
# Name the class
Object.const_set "FatMan", tweedledee.class
p tweedledee.class # => FatMan
tweedledee.class.send(:define_method, :say, yell)
tweedledum.singleton_class.send(:define_method, :say, whisper)
p heroes
# => [#<Object:0x990a584>, #<FatMan:0x990a598>]
p heroes.map(&:say.with("hello"))
# => [".....", "HELLO"]
p heroes.map{|x| x.say("hello")}
# => [".....", "HELLO"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment