Hi I have a file in /lib/test_class.rb whic looks like this module TestClass class TestClass def mehtod1 end def more methods .... end end in one of my controllers I have require ''test_class'' This seems to work, however when I do the next line to create an object it fails test = TestClass.new Any ideas where I am going wrong? -- Posted via http://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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Uh, you have to specify the module name... test = TestClass::TestClass.new Ruby may be smart, but it''s not prescient. Jason On 10/20/06, scot <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Hi > > I have a file in /lib/test_class.rb whic looks like this > > module TestClass > class TestClass > > def mehtod1 > end > > def more methods .... > > end > end > > in one of my controllers I have > > require ''test_class'' > > This seems to work, however when I do the next line to create an object > it fails > > test = TestClass.new > > Any ideas where I am going wrong? > > -- > Posted via http://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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Jason Roelofs wrote:> Uh, you have to specify the module name... > > test = TestClass::TestClass.new > > Ruby may be smart, but it''s not prescient. > > JasonExactly. Remember that when you say: TestClass.new you are actually calling the "new" method on the module, not the class. To actually get the class you have to dig into it with the "::" operator. TestClass::TestClass.new However, if you simply want to inlcude the class in your app you dont need to wrap it in a module at all. And then you dont need the :: at all. -- Posted via http://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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Alex Wayne wrote:> you are actually calling the "new" method on the module, not the class. > To actually get the class you have to dig into it with the "::" > operator. > > TestClass::TestClass.newcool thanks, whats the advantage of wrapping the code in a module, i''ve always done it, but not sure why? one last thing How can I call a function on that class if I have created an object pTest = TestClass::TestClass.new Should I not be able to do this, if there is a method inside the class called test? pTest.test if getting the error (private method `test'' called) -- Posted via http://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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Please read this: http://www.rubycentral.com/book/ Jason On 10/20/06, scot <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Alex Wayne wrote: > > you are actually calling the "new" method on the module, not the class. > > To actually get the class you have to dig into it with the "::" > > operator. > > > > TestClass::TestClass.new > > > cool thanks, whats the advantage of wrapping the code in a module, i''ve > always done it, but not sure why? > > one last thing > > How can I call a function on that class > > if I have created an object > pTest = TestClass::TestClass.new > > Should I not be able to do this, if there is a method inside the class > called test? > > pTest.test > > if getting the error (private method `test'' called) > > -- > Posted via http://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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---