Modules

O

Oliver Saunders

I understand the way these things are working but I don't really
understand what's going on and why.

module Foo
def bar
1
end
def Foo.zim
2
end
end


module SomeNamespace
require 'procedural_api'
end

SomeNamespace.function_from_procedural_api # this ain't gonna work!
SomeNamespace::function_from_procedural_api # nor is this

class SomeClass
include Foo
end

I'm very confused by all this. Please help unravel the mess in my brain.
 
M

matt neuburg

Oliver Saunders said:
I understand the way these things are working but I don't really
understand what's going on and why.

module Foo
def bar
1
end
def Foo.zim
2
end
end



class SomeClass
include Foo
end
I'm very confused by all this. Please help unravel the mess in my brain.

I'd say you're not confused at all. You've described perfectly how the
language behaves.

m.
 
R

Rimantas Liubertas

Foo.zim # works, OK, fair enough
objec.method works for any object. Everything is an object, so Foo is
an object too.
Class::method, Module::method works with class/module methods only.

Let's take a different angle:
=> nil
NoMethodError: undefined method `qux' for #<Baz:0x35c6f4>
from (irb):7
from :0


Thus zim exists only for the object of the Foo module - not any class
that mixes in Foo, just for THE Foo itself.


Regards,
Rimantas
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,263
Messages
2,571,062
Members
48,769
Latest member
Clifft

Latest Threads

Top