Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Curious include behavior

Reply
Thread Tools

Curious include behavior

 
 
Tom Werner
Guest
Posts: n/a
 
      09-28-2007
This caused me a small headache the other day. By including a module, an
instance method suddenly becomes accessible as a module method. Seems
unintuitive, I'm curious to understand the reasoning behind it.

module Foo
module Bar
def hello
'hello'
end
end
end

Foo::Bar.hello # => -:9: undefined method `hello' for Foo::Bar:Module (NoMethodError)

include Foo::Bar

Foo::Bar.hello # => "hello"


--
Tom Preston-Werner

* Libraries:
Chronic (chronic.rubyforge.org)
God (god.rubyforge.org)
Fuzed (fuzed.rubyforge.org)
* Site:
rubyisawesome.com


 
Reply With Quote
 
 
 
 
David A. Black
Guest
Posts: n/a
 
      09-28-2007
Hi --

On Sat, 29 Sep 2007, Tom Werner wrote:

> This caused me a small headache the other day. By including a module, an
> instance method suddenly becomes accessible as a module method. Seems
> unintuitive, I'm curious to understand the reasoning behind it.
>
> module Foo
> module Bar
> def hello
> 'hello'
> end
> end
> end
>
> Foo::Bar.hello # => -:9: undefined method `hello' for Foo::Bar:Module
> (NoMethodError)
>
> include Foo::Bar
>
> Foo::Bar.hello # => "hello"


At the top level, the include will include the module in Object:

ruby -e 'include Module.new; p Object.ancestors'
[Object, #<Module:0x2592c>, Kernel]

whereupon all objects will respond to its methods.


David

--
Upcoming training from Ruby Power and Light, LLC:
* Intro to Ruby on Rails, Edison, NJ, October 23-26
* Advancing with Rails, Edison, NJ, November 6-9
Both taught by David A. Black.
See http://www.rubypal.com for more info!

 
Reply With Quote
 
 
 
 
7stud --
Guest
Posts: n/a
 
      09-29-2007
David A. Black wrote:
>
> At the top level, the include will include the module in Object:
>


Where is the module before it is included?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
David A. Black
Guest
Posts: n/a
 
      09-29-2007
Hi --

On Sat, 29 Sep 2007, 7stud -- wrote:

> David A. Black wrote:
>>
>> At the top level, the include will include the module in Object:
>>

>
> Where is the module before it is included?


I'm not sure what you mean. Can you elaborate?


David

--
Upcoming training from Ruby Power and Light, LLC:
* Intro to Ruby on Rails, Edison, NJ, October 23-26
* Advancing with Rails, Edison, NJ, November 6-9
Both taught by David A. Black.
See http://www.rubypal.com for more info!

 
Reply With Quote
 
7stud --
Guest
Posts: n/a
 
      09-29-2007
David A. Black wrote:
> Hi --
>
> On Sat, 29 Sep 2007, 7stud -- wrote:
>
>> David A. Black wrote:
>>>
>>> At the top level, the include will include the module in Object:
>>>

>>
>> Where is the module before it is included?

>
> I'm not sure what you mean. Can you elaborate?
>
>
> David


I would think the name Foo would be visible just like the name of a
method is visible, e.g.:

def greet
puts "hello"
end

According to pickaxe2 p. 346, greet is inserted as a private method of
Object, and hence can be called in any context without a receiver, i.e.
without 'some_obj." in front of the method name.

In the op's example, there is a module named Foo. Is Foo just a free
floating object? Or, is it an attribute of some omnipresent object?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
David A. Black
Guest
Posts: n/a
 
      09-29-2007
Hi --

On Sat, 29 Sep 2007, 7stud -- wrote:

> David A. Black wrote:
>> Hi --
>>
>> On Sat, 29 Sep 2007, 7stud -- wrote:
>>
>>> David A. Black wrote:
>>>>
>>>> At the top level, the include will include the module in Object:
>>>>
>>>
>>> Where is the module before it is included?

>>
>> I'm not sure what you mean. Can you elaborate?
>>
>>
>> David

>
> I would think the name Foo would be visible just like the name of a
> method is visible, e.g.:
>
> def greet
> puts "hello"
> end
>
> According to pickaxe2 p. 346, greet is inserted as a private method of
> Object, and hence can be called in any context without a receiver, i.e.
> without 'some_obj." in front of the method name.
>
> In the op's example, there is a module named Foo. Is Foo just a free
> floating object? Or, is it an attribute of some omnipresent object?


Constants you define at the top level are owned by Object:

class C
end

module M
end

X = 1

p Object.constants.grep(/^(C|M|X)$/) # => ["X", "C", "M"]


David

--
Upcoming training from Ruby Power and Light, LLC:
* Intro to Ruby on Rails, Edison, NJ, October 23-26
* Advancing with Rails, Edison, NJ, November 6-9
Both taught by David A. Black.
See http://www.rubypal.com for more info!

 
Reply With Quote
 
7stud --
Guest
Posts: n/a
 
      09-30-2007
David A. Black wrote:
>
> Constants you define at the top level are owned by Object:
>


Is an include always necessary to be able to call a module's instance
methods?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      09-30-2007
On 9/30/07, 7stud -- <(E-Mail Removed)> wrote:
> David A. Black wrote:
> >
> > Constants you define at the top level are owned by Object:
> >

>
> Is an include always necessary to be able to call a module's instance
> methods?


Yes I believe so, that is include or extend

irb(main):001:0> module M
irb(main):002:1> def a; 42 end
irb(main):003:1> end
=> nil
irb(main):009:0> m = M.instance_method("a")
=> #<UnboundMethod: M#a>
irb(main):010:0> m.bind("a").call
TypeError: bind argument must be an instance of M
from (irb):10:in `bind'
from (irb):10
from :0
irb(main):012:0> C = Class::new{ include M }
###################^^^^^^^
=> C
irb(main):013:0> m.bind(C.new).call
=> 42
irb(main):014:0> a="42"
=> "42"
irb(main):015:0> a.extend M
###########^^^^^^
=> "42"
irb(main):016:0> m.bind(a).call
=> 42

HTH
Robert

--
what do I think about Ruby?
http://ruby-smalltalk.blogspot.com/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
MiniTest: a curious case of include (1.8 vs 1.9) Simon Chiang Ruby 6 02-02-2009 08:01 PM
/* #include <someyhing.h> */ => include it or do not include it?That is the question .... Andreas Bogenberger C Programming 3 02-22-2008 10:53 AM
Curious template behavior, g++ and xlC and icpc ckhoge@gmail.com C++ 0 06-15-2005 01:08 AM
Curious string behavior mark Python 2 01-28-2004 06:25 PM
Curious OE6 behavior Rusty Lillico Computer Support 5 07-01-2003 08:13 PM



Advertisments