Skip to content

Instantly share code, notes, and snippets.

@Fonsan
Created May 9, 2012 18:24

Revisions

  1. Fonsan revised this gist May 14, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion tivoli.rb
    Original 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 #doargsend"
    puts "LOG #{args}"
    end
    "hello" + "other" # => 'helloother'
    LOG ['other']
  2. Fonsan revised this gist May 11, 2012. 1 changed file with 11 additions and 11 deletions.
    22 changes: 11 additions & 11 deletions tivoli.rb
    Original file line number Diff line number Diff line change
    @@ -1,28 +1,28 @@
    t = Tivoli.new(String.instance_method(:+))
    t.aspect :before { |time, args, &block|
    t.aspect :before do |time, args, &block|
    # log stuff here or
    puts "LOG #{args}"
    }
    puts "LOG #doargsend"
    end
    "hello" + "other" # => 'helloother'
    LOG ['other']

    t.aspect :before { |time, args, &block|
    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 { |prev, time, args, &block|
    t.filter :before do |prev, time, args, &block|
    # change result
    'nope'
    }
    end
    'hello' + 'other' # => 'nope'

    t.aspect :before {
    t.aspect :before do
    sleep 1
    }
    t.filter :after { |prev, time, args, &block|
    end
    t.filter :after do |prev, time, args, &block|
    prev + time
    }
    end
    "hello" + "other" # => 'helloother1000'
  3. Fonsan revised this gist May 11, 2012. 2 changed files with 31 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion order.txt
    Original 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=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>
    28 changes: 28 additions & 0 deletions tivoli.rb
    Original 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'
  4. Fonsan revised this gist May 11, 2012. 1 changed file with 0 additions and 0 deletions.
    Empty file added tivoli.rb
    Empty file.
  5. Fonsan revised this gist May 11, 2012. 1 changed file with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions json_parse.rb
    Original 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))



  6. Fonsan revised this gist May 11, 2012. 1 changed file with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions order.txt
    Original 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>
  7. Fonsan revised this gist May 10, 2012. 7 changed files with 86 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions blocks.rb
    Original 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 }
    22 changes: 22 additions & 0 deletions method_module.rb
    Original 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
    12 changes: 12 additions & 0 deletions named_callbacks.js
    Original 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'
    9 changes: 9 additions & 0 deletions passing_procs.rb
    Original 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'
    9 changes: 9 additions & 0 deletions twice.rb
    Original 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
    5 changes: 5 additions & 0 deletions twice_proc.rb
    Original 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
    21 changes: 21 additions & 0 deletions twice_unbind.rb
    Original 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
  8. Fonsan created this gist May 9, 2012.
    7 changes: 7 additions & 0 deletions blocks.rb
    Original 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