Professional Web Applications Themes

Modules defining class methods to include? - Ruby

irb(main):001:0> module Foo irb(main):002:1> def foo irb(main):003:2> "foo_instance" irb(main):004:2> end irb(main):005:1> def Foo.foo irb(main):006:2> "foo_class" irb(main):007:2> end irb(main):008:1> end => nil irb(main):009:0> class Bar irb(main):010:1> include Foo irb(main):011:1> end => Bar irb(main):012:0> Bar.new.foo => "foo_instance" irb(main):013:0> Bar.foo NoMethodError: undefined method `foo' for Bar:Class from (irb):13 How do I get Bar to "inherit" Foo.foo? I've done it before like this: irb(main):001:0> module Foo irb(main):002:1> def foo irb(main):003:2> "foo_instance" irb(main):004:2> end irb(main):005:1> def Foo.included(aModule) irb(main):006:2> super irb(main):007:2> aModule.instance_eval do irb(main):008:3* def foo irb(main):009:4> "foo_class" irb(main):010:4> end irb(main):011:3> end irb(main):012:2> end irb(main):013:1> end => nil irb(main):014:0> class Bar irb(main):015:1> include Foo irb(main):016:1> end ...

  1. #1

    Default Modules defining class methods to include?

    irb(main):001:0> module Foo
    irb(main):002:1> def foo
    irb(main):003:2> "foo_instance"
    irb(main):004:2> end
    irb(main):005:1> def Foo.foo
    irb(main):006:2> "foo_class"
    irb(main):007:2> end
    irb(main):008:1> end
    => nil
    irb(main):009:0> class Bar
    irb(main):010:1> include Foo
    irb(main):011:1> end
    => Bar
    irb(main):012:0> Bar.new.foo
    => "foo_instance"
    irb(main):013:0> Bar.foo
    NoMethodError: undefined method `foo' for Bar:Class
    from (irb):13

    How do I get Bar to "inherit" Foo.foo? I've done it before like this:

    irb(main):001:0> module Foo
    irb(main):002:1> def foo
    irb(main):003:2> "foo_instance"
    irb(main):004:2> end
    irb(main):005:1> def Foo.included(aModule)
    irb(main):006:2> super
    irb(main):007:2> aModule.instance_eval do
    irb(main):008:3* def foo
    irb(main):009:4> "foo_class"
    irb(main):010:4> end
    irb(main):011:3> end
    irb(main):012:2> end
    irb(main):013:1> end
    => nil
    irb(main):014:0> class Bar
    irb(main):015:1> include Foo
    irb(main):016:1> end
    => Bar
    irb(main):017:0> Bar.new.foo
    => "foo_instance"
    irb(main):018:0> Bar.foo
    => "foo_class"

    But that's rather ugly. Surely there's a better way?

    ruby 1.8.0 (2003-08-04) [i686-linux-gnu]

    Tim.

    --
    Tim Bates
    id.au


    Tim Guest

  2. #2

    Default Re: Modules defining class methods to include?

    Tim Bates wrote: 

    class Bar
    include Foo
    extend Foo
    end


    Joel Guest

  3. #3

    Default Re: Modules defining class methods to include?

    Joel VanderWerf wrote: 

    irb(main):017:0> Bar.new.foo
    => "foo_instance"
    irb(main):018:0> Bar.foo
    => "foo_instance"

    Not what I wanted.

    --
    Tim Bates
    id.au


    Tim Guest

  4. #4

    Default Re: Modules defining class methods to include?

    Tim Bates wrote: 
    >
    >
    > irb(main):017:0> Bar.new.foo
    > => "foo_instance"
    > irb(main):018:0> Bar.foo
    > => "foo_instance"
    >
    > Not what I wanted.
    >[/ref]

    Oops. But you can do something like this:

    module Foo
    def foo
    "foo_instance"
    end
    module Sub
    def foo
    "foo_class"
    end
    end
    extend Sub # so Foo.foo still works
    def Foo.included(aModule)
    super
    aModule.instance_eval do
    extend Sub
    end
    end
    end

    class Bar
    include Foo
    end

    p Bar.new.foo

    p Bar.foo



    Joel Guest

Similar Threads

  1. defining a simple class
    By dfduiud33 in forum Macromedia Flex General Discussion
    Replies: 2
    Last Post: April 16th, 05:22 AM
  2. Replies: 5
    Last Post: November 26th, 07:13 PM
  3. Instance- and class-variables (was mixing in class methods)
    By dblack@superlink.net in forum Ruby
    Replies: 2
    Last Post: October 3rd, 03:52 AM
  4. Instance- and class-variables (was mixing in class methods)
    By dblack@superlink.net in forum Ruby
    Replies: 0
    Last Post: October 2nd, 04:19 PM
  5. Instance- and class-variables (was mixing in class methods)
    By dblack@superlink.net in forum Ruby
    Replies: 0
    Last Post: October 2nd, 12:46 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139