The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Tricking that old, picky interpreter: prototype-based OOP

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.
Tricking that old, picky interpreter: prototype-based OOP Posted: Feb 15, 2006 8:38 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Eigen Class.
Original Post: Tricking that old, picky interpreter: prototype-based OOP
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

Ruby's object model has been becoming more strict as of late, preventing some imaginative (albeit ultimately useless?) tricks like the following prototypish OOP:

a = Object.new
def a.foo; "a#foo" end
a.foo                                              # => "a#foo"
ProtoA = Class.new(class << a; self.dup end)
b = ProtoA.new
b.foo                                              # => "a#foo"
RUBY_VERSION                                       # => "1.8.2"

Nowadays, that snippet would die on the Class#dup of the singleton class:

a = Object.new
def a.foo; "a#foo" end
a.foo                                              # => "a#foo"
ProtoA = Class.new(class << a; self.dup end)
b = ProtoA.new
b.foo                                              # => 
# ~> -:4:in `initialize_copy': can't copy singleton class (TypeError)
# ~> 	from -:4

This doesn't mean we cannot do it, though, but it requires some black magic:

a = "foo"
def a.bar; "A#bar" end
proto = a.prototype
proto                                              # => #<Class:0xb7d43f50>
proto.superclass                                   # => #<Class:#<String:0xb7d45544>>
orig = String.instance_methods
proto.instance_methods(true) - orig                # => ["bar"]

ueber_string = proto.new
ueber_string                                       # => ""
ueber_string.bar                                   # => "A#bar"

proto.class_eval do
  def initialize(x); super(x.to_s.upcase) end
end

proto.new("hello, world")                          # => "HELLO, WORLD"

object = Object.new
class << object
  def foo; "object#foo" end
end

obj = object.prototype.new
obj.foo                                            # => "object#foo"

def object.bar; "object#bar" end
obj.bar                                            # => "object#bar"

That's more powerful than a mere Class.new(singleton_class.dup) because changes in the singleton class affect descendents, as happens with normal inheritance (for both classes and modules).

Making it happen


Read more...

Read: Tricking that old, picky interpreter: prototype-based OOP

Topic: Is AJAX Insecure? Previous Topic   Next Topic Topic: Flickr Uploadr

Sponsored Links



Google
  Web Artima.com   

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