The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Yet another way to wrap methods in Ruby

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
Eigen Class

Posts: 358
Nickname: eigenclass
Registered: Oct, 2005

Eigenclass is a hardcore Ruby blog.
Yet another way to wrap methods in Ruby Posted: Dec 7, 2005 11:30 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Eigen Class.
Original Post: Yet another way to wrap methods in Ruby
Feed Title: Eigenclass
Feed URL: http://feeds.feedburner.com/eigenclass
Feed Description: Ruby stuff --- trying to stay away from triviality.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Eigen Class
Latest Posts From Eigenclass

Advertisement

Here's another way to wrap methods, reminiscent of "cuts". It can be used to do things like

class Foo
  def foo; "Foo#foo" end
end
class B < Foo
  def foo; "B#foo " + super end
end
class C < B
  def foo; "C#foo " + super end
end

b = B.new
c = C.new
b.foo                                              # => "B#foo Foo#foo"
c.foo                                              # => "C#foo B#foo Foo#foo"
Foo.replace{ def foo; "New#foo [" + super + "]" end }
b.foo                                              # => "B#foo New#foo [Foo#foo]"
c.foo                                              # => "C#foo B#foo New#foo [Foo#foo]"
B.replace{ def foo; "<" + super + ">" end }
b.foo                                              # => "<B#foo New#foo [Foo#foo]>"
c.foo                                              # => "C#foo <B#foo New#foo [Foo#foo]>"
Foo.replace{ def foo; "New2#foo [" + super + "]" end }
b.foo                                              # => "<B#foo New2#foo [Foo#foo]>"
c.foo                                              # => "C#foo <B#foo New2#foo [Foo#foo]>"

My implementation uses a special class which is made right after the user creates a new class, and derives from the latter: in that class, super will correspond to the original method definition. This means that, in the above example, the klass chain would look like this:

c.class.ancestors                                  # => [C, C, B, B, Foo, Foo, Object, Kernel]


Read more...

Read: Yet another way to wrap methods in Ruby

Topic: Ruport on Rails??? Previous Topic   Next Topic Topic: Splat is good for you

Sponsored Links



Google
  Web Artima.com   

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