Where do I put a custom class? How do I use it? -- Posted via ruby-forum.com.
2009/10/6 Pål Bergström <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > Where do I put a custom class?It depends on what is it''s purpose but, usually, you''ll put them on your models directory.> How do I use it?Just like you use every class. -- Leonardo Mateo. There''s no place like ~
Leonardo Mateo wrote:> 2009/10/6 P�l Bergstr�m <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>: >> >> Where do I put a custom class? > It depends on what is it''s purpose but, usually, you''ll put them on > your models directory. > >> How do I use it? > Just like you use every class. > > -- > Leonardo Mateo. > There''s no place like ~I can''t make it work. I''ve put in the lib directory. Do I need to make a require or something? I get an error "private method `test'' called for ...". -- Posted via ruby-forum.com.
Hi Pål Bergström Can you paste the code? Sijo -- Posted via ruby-forum.com.
2009/10/6 Pål Bergström <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > Leonardo Mateo wrote: >> 2009/10/6 P�l Bergstr�m <rails-mailing-list-ARtvInVfO7m5VldFQK4jKA@public.gmane.orgt>: >>> >>> Where do I put a custom class? >> It depends on what is it''s purpose but, usually, you''ll put them on >> your models directory. >> >>> How do I use it? >> Just like you use every class. >> >> -- >> Leonardo Mateo. >> There''s no place like ~ > > I can''t make it work. I''ve put in the lib directory. Do I need to make a > require or something? I get an error "private method `test'' called forOf course you have to require them. Probably on your environment.rb if you want to require it only once, but you should see if that worth the payload. If the class is "too heavy"you should require it only when you''re going to use it. -- Leonardo Mateo. There''s no place like ~
Sijo kg wrote:> Hi Pål Bergström > > Can you paste the code? > > SijoIt''s just a test code. In the file /lib/random.rb I have: class Random def test "test" end end In the controller I try to call: @test = Random.test -- Posted via ruby-forum.com.
Leonardo Mateo wrote:> 2009/10/6 Pål Bergström <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>: >>> >>> -- >>> Leonardo Mateo. >>> There''s no place like ~ >> >> I can''t make it work. I''ve put in the lib directory. Do I need to make a >> require or something? I get an error "private method `test'' called for > Of course you have to require them. Probably on your environment.rb if > you want to require it only once, but you should see if that worth the > payload. If the class is "too heavy"you should require it only when > you''re going to use it. > > > -- > Leonardo Mateo. > There''s no place like ~I see. I have another solution, a custom Crypto-class, in another app similar to this without a require (as I can see now, was a while ago) and that works. It''s also in /lib. Can''t understan why that works and now it doesn''t. I thought that custom classes in /lib or /model was included by default without require. -- Posted via ruby-forum.com.
Pål Bergström wrote:> I see. I have another solution, a custom Crypto-class, in another app > similar to this without a require (as I can see now, was a while ago) > and that works. It''s also in /lib. Can''t understan why that works and > now it doesn''t. > > I thought that custom classes in /lib or /model was included by default > without require.Sorry. The Crypto thing was a module not a class. -- Posted via ruby-forum.com.
2009/10/6 Pål Bergström <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > Sijo kg wrote: >> Hi Pål Bergström >> >> Can you paste the code? >> >> Sijo > > It''s just a test code. In the file /lib/random.rb I have: > > class Random > > def test > "test" > end > > end > > In the controller I try to call: > > @test = Random.testIf you want to call it like that you must make test a class method rather than an instance method, so it must be def self.test .. Colin> -- > Posted via ruby-forum.com. > > > >
Colin Law wrote:> 2009/10/6 Pål Bergström <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> If you want to call it like that you must make test a class method > rather than an instance method, so it must be > def self.test > .. > > ColinThanks. I don''t fully understand but it works. This means you don''t need to require it (?) -- Posted via ruby-forum.com.
2009/10/6 Pål Bergström <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > Colin Law wrote: >> 2009/10/6 Pål Bergström <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>: > >> If you want to call it like that you must make test a class method >> rather than an instance method, so it must be >> def self.test >> .. >> >> Colin > > Thanks. I don''t fully understand but it works. > > This means you don''t need to require it (?)Whether it is a class or instance method is nothing to do with requiring or not, it is just a fundamental of Ruby syntax. Colin> -- > Posted via ruby-forum.com. > > > >
Hi> Thanks. I don''t fully understand but it works. > > This means you don''t need to require it (?)No require needed .And to get a good understanding of class and instance methods in Ruby read railstips.org/2009/5/11/class-and-instance-methods-in-ruby Sijo -- Posted via ruby-forum.com.
Sijo kg wrote:> railstips.org/2009/5/11/class-and-instance-methods-in-ruby >That was great. Thank you. Still not sure about instance and what it means. I''ve encountered a lot in my Rails life but one of those things that doesn''t stick. Probably why I''m a web designer and developer struggling to be a programmer but lack some nerd cells to be really good at it. I''m pretty fluent in my Rails development but still have to look up things every day. This time I felt I must learn how to make my own Classes, and plugins, as that will be easier to reuse stuff. -- Posted via ruby-forum.com.
Hi Pål Bergström> That was great. Thank you. Still not sure about instance and what it > means.It is not a difficult thing to grasp Just an example Suppose you have a class Fruit.Now an apple and an orange are instances of the class Fruit. So in Ruby it can be created like apple = Fruit.new or orange = Fruit.new Thousands of other examples.You have to read any object oriented book Sijo -- Posted via ruby-forum.com.
Sijo kg wrote:> It is not a difficult thing to grasp Just an example Suppose you > have a class Fruit.Now an apple and an orange are instances of the class > Fruit. So in Ruby it can be created like > > apple = Fruit.new or > orange = Fruit.new > > > Thousands of other examples.You have to read any object oriented > book > > > SijoNow the coin fell down. Got it. -- Posted via ruby-forum.com.