-
-
Save ivaravko/6006703 to your computer and use it in GitHub Desktop.
This file contains 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
def do_something | |
yield 'something' | |
end | |
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 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
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)) | |
This file contains 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 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 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
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 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
<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> | |
<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 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
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 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
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'] | |
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' |
This file contains 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 Numeric | |
def twice | |
self * 2 | |
end | |
end | |
2.twice # => 4 | |
1.5.twice # => 3.0 |
This file contains 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
twice = proc {self * 2} | |
Numeric.send(:define_method, :twice, &twice) | |
2.twice |
This file contains 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 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment