Custom class

Where do I put a custom class? How do I use it?

Leonardo M. wrote:

2009/10/6 P�l Bergstr�m [email protected]:

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 M…
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
…”.

Hi Pål Bergström

Can you paste the code?

Sijo

2009/10/6 Pål Bergström [email protected]:

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 M…
There’s no place like ~

2009/10/6 PÃ¥l Bergström [email protected]:


Leonardo M…
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 M…
There’s no place like ~

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.test

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.

Leonardo M. wrote:

2009/10/6 PÃ¥l Bergström [email protected]:


Leonardo M…
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 M…
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.

Colin L. wrote:

2009/10/6 PÃ¥l Bergström [email protected]:

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 (?)

2009/10/6 PÃ¥l Bergström [email protected]:

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

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

http://railstips.org/2009/5/11/class-and-instance-methods-in-ruby

Sijo

2009/10/6 PÃ¥l Bergström [email protected]:

class Random

 def test
  “test”
 end

end

In the controller I try to call:

@test = Random.test

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

Sijo kg wrote:

http://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.

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

Sijo

Now the coin fell down. Got it.

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs