On Mar 26, 2:39 am, Patrick Clas
<li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>
wrote:> I tried creating a module in /lib/my_foo/foo_bar.rb called module
> MyFoo::FooBar. Inside that module I defined a method getFoo(). I
> modified application.rb and added the line config.autoload_paths +>
%W(#{Rails.root}/lib).
>
> When I tried to access MyFoo::FooBar.getFoo() in my controller I got the
> following error:
> undefined method `getFoo'' for MyFoo::FooBar:Module
>
> I realized that I didn''t make getFoo a class method so I tried
changing
> the method definition to MyFoo::FooBar.getFoo(), but then I got the
> following error:
>
> syntax error, unexpected ''.'', expecting
''\n'' or '';''
> def MyFoo::FooBar.getFoo()
>
> I then tried changing the method definition to self.getFoo() and
> everything works fine. I also tried moving foo_bar.rb directly into
> /lib, changed the module name to FooBar and the method definition to
> FooBar.getFoo() and that also worked. My question is why? I assume I
> just don''t have the correct syntax for specifying the module name
when
> it''s in one or more sub-directories off of /lib instead of
directly in
> /lib, but I can''t figure out what the correct syntax should be.
>
> I''m fine with using "self", but I''d like to
understand what I''m doing
> wrong.
>
You''re finding the module fine (if you weren''t the error
you''d get
something along the lines of uninitialized constant MyFoo), you''re
just not calling the method right.
given
module Foo
def bar
end
end
Then you can''t do Foo.bar, it just doesn''t work that way
(it''s like
trying to call a classes instance method on the class itself. Doing
def self.bar or def Foo.bar makes it a singleton method, so you can
then do Foo.bar
with a module nested in another you can''t write
module Foo
module Bar
def Foo::Bar.foo
end
end
end
it''s just not syntactically valid. You can write
module Foo
module Bar
def Bar.foo
end
end
end
which does create Foo::Bar.foo
Fred> --
> Posted viahttp://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.