Created
August 28, 2013 01:21
-
-
Save JonathonMA/6361079 to your computer and use it in GitHub Desktop.
Classy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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