[prev in list] [next in list] [prev in thread] [next in thread] 

List:       ruby-talk
Subject:    Re: Is better to subclass or to add methods to an existing class?
From:       Paul Brannan <pbrannan () atdesk ! com>
Date:       2002-09-20 0:42:34
[Download RAW message or body]

On Fri, Sep 20, 2002 at 07:38:40AM +0900, Christoph wrote:
> ``proc type'' class_eval's  do not change the scope. In other
> words there is only one (the top) Beast around - hence

This must have changed from 1.6 to 1.7.

  / test.rb
    Beast = self
    (@temptation = Class.new String).class_eval <<-Body
    Beast = self
    def six_six_six; Beast end
    Body

    t = @temptation.new("foo")
    p t.six_six_six
    p Beast

    Beast = self
    (@temptation = Class.new String).class_eval do
      Beast = self
      def six_six_six; Beast end
    end

    t = @temptation.new("foo")
    p t.six_six_six
    p Beast
  \ end test.rb

  [pbrannan@zaphod tmp]$ ruby -v test.rb
  ruby 1.6.7 (2002-03-01) [i686-linux]
  #<Class 0lx4028b8b8>
  #<Object:0x40296ce0 @temptation=#<Class 0lx4028b8b8>>
  test.rb:11: warning: already initialized constant Beast
  #<Class 0lx402892fc>
  #<Object:0x40296ce0 @temptation=#<Class 0lx402892fc>>

  [pbrannan@zaphod tmp]$ ruby-1.7 -v test.rb
  ruby 1.7.3 (2002-09-04) [i686-linux]
  #<Class:0x401d2154>
  #<Object:0x401e0a74 @temptation=#<Class:0x401d2154>>
  test.rb:11: warning: already initialized constant Beast
  test.rb:13: warning: already initialized constant Beast
  Beast
  Beast

> A ``string type'' class_eval creates the expected two
> Beasts and six_sx_six picks up the inner Beast - hence

I guess the string-type eval will work on both 1.6 and 1.7.  I wonder if
there's a way to get the same functionality on both 1.6 and 1.7 without
evaling a string.

Paul

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic