The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Ruby snippet: detaching a method from an object as a closure

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Jeremy Voorhis

Posts: 212
Nickname: jvoorhis
Registered: Oct, 2005

Jeremy Voorhis is a Rubyist in northeast Ohio.
Ruby snippet: detaching a method from an object as a closure Posted: Jun 15, 2006 6:30 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Jeremy Voorhis.
Original Post: Ruby snippet: detaching a method from an object as a closure
Feed Title: JVoorhis
Feed URL: http://feeds.feedburner.com/jvoorhis
Feed Description: JVoorhis is a Rubyist in northeast Ohio. He rambles about Ruby on Rails, development practices, other frameworks such as Django, and on other days he is just full of snark.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Jeremy Voorhis
Latest Posts From JVoorhis

Advertisement

irb(main):041:0> "cat".method( :size ).to_proc.call          
=> 3
irb(main):042:0> str = 'CAT'
=> "CAT" 
irb(main):043:0> aproc = str.method( :size ).to_proc
=> #<Proc:0x000cc064@(irb):43>
irb(main):044:0> aproc.call
=> 3
irb(main):045:0> aproc.call
=> 3
irb(main):046:0> str << 'FISH'
=> "CATFISH" 
irb(main):047:0> aproc.call
=> 7
irb(main):048:0> newstr = ''
=> "" 
irb(main):049:0> def newstr.metaclass; class << self; self; end; end # thx _why!
=> nil
irb(main):050:0> newstr.metaclass.send :define_method, :size, &aproc
=> #<Proc:0x000cc064@(irb):43>
irb(main):051:0> str
=> "CATFISH" 
irb(main):052:0> newstr
=> "" 
irb(main):053:0> str.size
=> 7
irb(main):054:0> newstr.size
=> 7

Read: Ruby snippet: detaching a method from an object as a closure

Topic: Slow require of ruport unconvered Previous Topic   Next Topic Topic: InfoQ Launch Update

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use