Custom class

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.

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 wrote:

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

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

Colin Law wrote:

Colin Law wrote:

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

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