Class instance method can't be defined in a module?

Discussion in 'Ruby' started by John Platte, Apr 16, 2004.

  1. John Platte

    John Platte Guest

    Trying to work some attr_accessor - style magic so I can refactor a
    bunch of menial methods into a single declaration. Can't subclass, so
    I'm trying to do it with a module.

    In the code below, I expect both of the "puts" lines to work on the
    "abc" string, but I can't figure out what I'm missing. Can anyone
    enlighten me here?

    module FooModule
    def delegate(*methods)
    methods.each {|method|
    module_eval %{
    def #{method}
    @new_receiver.#{method}
    end
    }
    }
    end
    module_function :delegate
    end

    class TryIt
    include FooModule
    FooModule.delegate :intern
    delegate :capitalize
    def initialize(obj)
    @new_receiver = obj
    end
    end

    t = TryIt.new("abc")
    puts t.intern
    puts t.capitalize

    =====
    $ ruby demo.rb
    demo.rb:17: undefined method `delegate' for TryIt:Class (NoMethodError)

    (now comment out the line "delegate :capitalize")

    $ ruby demo.rb
    abc
    demo.rb:25: undefined method `capitalize' for #<TryIt:0x283c8
    @new_receiver="abc"> (NoMethodError)

    --
    Ryan "John" Platte
    Custom services, NIKA Consulting
    http://nikaconsulting.com/
     
    John Platte, Apr 16, 2004
    #1
    1. Advertising

  2. On Sat, Apr 17, 2004 at 07:32:07AM +0900, John Platte wrote:
    > Trying to work some attr_accessor - style magic so I can refactor a
    > bunch of menial methods into a single declaration. Can't subclass, so
    > I'm trying to do it with a module.
    >
    > In the code below, I expect both of the "puts" lines to work on the
    > "abc" string, but I can't figure out what I'm missing. Can anyone
    > enlighten me here?


    batsman@tux-chan:/tmp$ cat fdgdf.rb
    module FooModule
    def delegate(*methods)
    methods.each do |method|
    define_method(method){|*a| @new_receiver.send(method, *a) }
    end
    end
    end

    class TryIt
    extend FooModule
    delegate :intern
    delegate :capitalize
    def initialize(obj)
    @new_receiver = obj
    end
    end

    t = TryIt.new("abc")
    puts t.intern
    puts t.capitalize
    batsman@tux-chan:/tmp$ ruby fdgdf.rb
    abc
    Abc

    --
    Running Debian GNU/Linux Sid (unstable)
    batsman dot geo at yahoo dot com

    The documentation is in Japanese. Good luck.
    -- Rich $alz
     
    Mauricio Fernández, Apr 16, 2004
    #2
    1. Advertising

  3. John Platte

    John Platte Guest

    On 2004 Apr 16, at 17:42, Mauricio Fernández wrote:

    >> Trying to work some attr_accessor - style magic so I can refactor a
    >> bunch of menial methods into a single declaration. Can't subclass, so
    >> I'm trying to do it with a module...I can't figure out what I'm
    >> missing...

    >
    > extend FooModule


    Thank you, Mauricio. This was indeed the fix I needed.

    I guess I don't understand the distinction between "include" and
    "extend" from the descriptions in the Pickaxe. There's apparently a
    conceptual difference?

    --
    Ryan "John" Platte
    Custom services, NIKA Consulting
    http://nikaconsulting.com/
     
    John Platte, Apr 19, 2004
    #3
  4. On Tue, Apr 20, 2004 at 06:21:27AM +0900, John Platte wrote:
    > On 2004 Apr 16, at 17:42, Mauricio Fernández wrote:
    >
    > >>Trying to work some attr_accessor - style magic so I can refactor a
    > >>bunch of menial methods into a single declaration. Can't subclass, so
    > >>I'm trying to do it with a module...I can't figure out what I'm
    > >>missing...

    > >
    > > extend FooModule

    >
    > Thank you, Mauricio. This was indeed the fix I needed.
    >
    > I guess I don't understand the distinction between "include" and
    > "extend" from the descriptions in the Pickaxe. There's apparently a
    > conceptual difference?


    obj.extend Foo

    works like

    class << obj; include Foo end


    --
    Running Debian GNU/Linux Sid (unstable)
    batsman dot geo at yahoo dot com

    Linux is obsolete
    -- Andrew Tanenbaum
     
    Mauricio Fernández, Apr 19, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sullivan WxPyQtKinter
    Replies:
    5
    Views:
    446
    Rene Pijlman
    Mar 7, 2006
  2. Oodini
    Replies:
    1
    Views:
    1,837
    Keith Thompson
    Sep 27, 2005
  3. Replies:
    1
    Views:
    160
    Trans
    Feb 5, 2007
  4. Raj Singh
    Replies:
    2
    Views:
    216
    Rick DeNatale
    May 29, 2008
  5. Greg Hauptmann
    Replies:
    9
    Views:
    265
    Loren Segal
    Jun 16, 2008
Loading...

Share This Page