Created
May 9, 2012 18:24
Revisions
-
Fonsan revised this gist
May 14, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ t = Tivoli.new(String.instance_method(:+)) t.aspect :before do |time, args, &block| # log stuff here or puts "LOG #{args}" end "hello" + "other" # => 'helloother' LOG ['other'] -
Fonsan revised this gist
May 11, 2012 . 1 changed file with 11 additions and 11 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,28 +1,28 @@ t = Tivoli.new(String.instance_method(:+)) t.aspect :before do |time, args, &block| # log stuff here or puts "LOG #doargsend" end "hello" + "other" # => 'helloother' LOG ['other'] t.aspect :before do |time, args, &block| # change arguments args[0].reverse! end 'hello' + 'other' # => 'helloretho' # prev is used for chaining multiple filters, first time prev is nil t.filter :before do |prev, time, args, &block| # change result 'nope' end 'hello' + 'other' # => 'nope' t.aspect :before do sleep 1 end t.filter :after do |prev, time, args, &block| prev + time end "hello" + "other" # => 'helloother1000' -
Fonsan revised this gist
May 11, 2012 . 2 changed files with 31 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -12,4 +12,6 @@ <div class="gist"><a href="https://gist.github.com/2647673.js?file=twice_unbind.rb">https://gist.github.com/2647673.js?file=twice_unbind.rb</a></div> <div class="gist"><a href="https://gist.github.com/2647673.js?file=twice_proc.rb">https://gist.github.com/2647673.js?file=twice_proc.rb</a></div> <div class="gist"><a href="https://gist.github.com/2647673.js?file=tivoli.rb">https://gist.github.com/2647673.js?file=tivoli.rb</a></div> 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,28 @@ t = Tivoli.new(String.instance_method(:+)) t.aspect :before { |time, args, &block| # log stuff here or puts "LOG #{args}" } "hello" + "other" # => 'helloother' LOG ['other'] t.aspect :before { |time, args, &block| # change arguments args[0].reverse! } 'hello' + 'other' # => 'helloretho' # prev is used for chaining multiple filters, first time prev is nil t.filter :before { |prev, time, args, &block| # change result 'nope' } 'hello' + 'other' # => 'nope' t.aspect :before { sleep 1 } t.filter :after { |prev, time, args, &block| prev + time } "hello" + "other" # => 'helloother1000' -
Fonsan revised this gist
May 11, 2012 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
Empty file. -
Fonsan revised this gist
May 11, 2012 . 1 changed file with 15 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,15 @@ require 'json' json_rows = '{ "attr" : 4365345 } { "attr" : 543455 } { "attr" : 634543545 } { "attr" : 1231235432 } { "attr" : 3249882340 } { "attr" : 923489324 }' data = json_rows. split("\n"). map(&JSON.method(:parse)) -
Fonsan revised this gist
May 11, 2012 . 1 changed file with 15 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,15 @@ <div class="gist"><a href="https://gist.github.com/2647673.js?file=blocks.rb">https://gist.github.com/2647673.js?file=blocks.rb</a></div> <div class="gist"><a href="https://gist.github.com/2647673.js?file=named_callbacks.js">https://gist.github.com/2647673.js?file=named_callbacks.js</a></div> <div class="gist"><a href="https://gist.github.com/2647673.js?file=passing_procs.rb">https://gist.github.com/2647673.js?file=passing_procs.rb</a></div> <div class="gist"><a href="https://gist.github.com/2647673.js?file=json_parse.rb">https://gist.github.com/2647673.js?file=json_parse.rb</a></div> <div class="gist"><a href="https://gist.github.com/2647673.js?file=twice.rb">https://gist.github.com/2647673.js?file=twice.rb</a></div> <div class="gist"><a href="https://gist.github.com/2647673.js?file=twice_unbind.rb">https://gist.github.com/2647673.js?file=twice_unbind.rb</a></div> <div class="gist"><a href="https://gist.github.com/2647673.js?file=twice_proc.rb">https://gist.github.com/2647673.js?file=twice_proc.rb</a></div> -
Fonsan revised this gist
May 10, 2012 . 7 changed files with 86 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -5,3 +5,11 @@ def do_something do_something do |thing| puts thing end def do_complex(first, second) thing = 'nothing' first.call(thing) second.call(thing) end do_complex proc {|arg| puts arg }, proc {|arg| puts arg.reverse } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,22 @@ class Object def include_method(clazz, method_name) proxy = proc { |*args,&block| clazz.instance_method(method_name). bind(self). call(*args, &block) } functionality = Module.new functionality.send(:define_method, method_name, &proxy) self.send(:include, functionality) end end class Numeric def twice self * 2 end end String.include_method(Numeric, :twice) "ho ".twice # Fail 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,12 @@ var foo = function(first, second) { var thing = 'something' first(second(thing)); } var twice = function(string) { return string + string; } var suffix = function(string) { return string.substring(2) } foo(suffix, twice); // => 'methingsomething' 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,9 @@ foo = proc { |string| string.reverse.upcase } ['one', 'two', 'three'].map(&foo) # => ["ENO", "OWT", "EERHT"] foo.call('four') # => 'RUOF' #or foo['five'] # => 'EVIF' 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,9 @@ class Numeric def twice self * 2 end end 2.twice # => 4 1.5.twice # => 3.0 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,5 @@ twice = proc {self * 2} Numeric.send(:define_method, :twice, &twice) 2.twice 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,21 @@ class Numeric def twice self * self end end unbound_twice = Numeric.instance_method(:twice) bound_2_twice = unbound_twice.bind(2) bound_2_twice.call # => 4 # Expecting "ho ho " unbound_twice.bind("ho ") # => TypeError: bind argument must be an instance of Numeric def unbound_twice.owner String end unbound_twice.bind("ho ") # => TypeError: bind argument must be an instance of Numeric # Clearly some evil C voodoo magic -
Fonsan created this gist
May 9, 2012 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,7 @@ def do_something yield 'something' end do_something do |thing| puts thing end