Define and call methods in ruby

Hi, I just simply can’t figure out how I can define methods and call it
in a class. I’ve tried(as I read so) the following:

class Myclass

method_to_call

def method_to_call

end
end

but just doesn’t work. So, please point me out on where I’m doing wrong.
thanks…

Hi –

On Thu, 11 Sep 2008, Jay P. wrote:

end

but just doesn’t work. So, please point me out on where I’m doing wrong.
thanks…

This will get you started:

class Myclass
def method_to_call
puts “Hello!”
end
end

object = Myclass.new
object.method_to_call

It’s the object (the instance of Myclass) that can execute the method.

David

David A. Black wrote:

This will get you started:

class Myclass
def method_to_call
puts “Hello!”
end
end

object = Myclass.new
object.method_to_call

It’s the object (the instance of Myclass) that can execute the method.

David

Thanks David for the help, but just kinda suprised, wouldn’t it be for
method that’s being called from another class (no offense, just my view
from my lil knowledge of java). If what you are saying is what should be
for method calls in the same class then can you please lighten me up a
bit coz I’m new to ruby as well. I mean method calls as:

class myclass
if something then
do_something
end

#implementation of do_something method.
def do_something
here something is done…
end
end

Thanks again…

You have to distinguish between instance methods and class methods.

You appear to be asking how to create a class method. To do that you use
self.do_something:

class Myclass
if something then
do_something
end

def self.do_something
here something is done…
end
end

Hi –

On Thu, 11 Sep 2008, DanDiebolt.exe wrote:

def self.do_something
here something is done…
end
end

Though you’d have to define the method before you call it.

David

2008/9/11 Jay P. [email protected]:

object.method_to_call

It’s the object (the instance of Myclass) that can execute the method.

David

Thanks David for the help, but just kinda suprised, wouldn’t it be for
method that’s being called from another class (no offense, just my view
from my lil knowledge of java). If what you are saying is what should be
for method calls in the same class then can you please lighten me up a

What?

end
end

You are mixing instance and class methods. When you do

class X
if expr then
do_something
end
end

do_somehing must be a class method because inside class…end self is
the class. Try it out:

class Foo
p self

def x
p self
end
end

Foo.new.x

From what you write I am unsure whether basic OO principles are clear
to you. In case not, you should probably read some introductory
material about OOA/OOD/OOP.

Cheers

robert

maybe I’m wrong. but what I’m saying is (Here’s what I do in java)

public class myclass
{

String name;
static int i=0;
//defining a instance method
public void setName(String name)
{
this.name=name;
}
//defining the method.
public void printMessage()
{
System.out.println(“Message”);
}
//for some event
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==xxxx)
{
printMessage(); //declaring a method in the same class.
}
}

}

public class anotherclass extends myclass
{
myclass mc;
int j;

mc.setName(“James”); //calls method with the instance of the class
j=myclass.i; //here coz i belongs to class.
}

Hope, it clears out what I’m trying to do. Just example, not very good
in java either…
So, what I’m trying to do is declare similar method as printMessage() in
ruby. thanks

Hi –

On Thu, 11 Sep 2008, Jay P. wrote:

object.method_to_call
bit coz I’m new to ruby as well. I mean method calls as:
end
================================================================================

You’re defining do_something as an instance method of Myclass; that
means that you need an instance of Myclass in order to call
do_something. To get that instance, you need to do Myclass.new.

David

On Thu, Sep 11, 2008 at 4:37 PM, Jay P. [email protected] wrote:

class MyDate
md=MyDate.new()
md.display
def display
puts “hi”
end
end
look at David’s original post again

David A. Black wrote:

You’re defining do_something as an instance method of Myclass; that
means that you need an instance of Myclass in order to call
do_something. To get that instance, you need to do Myclass.new.

David

Thanks again David, So, here’s how I’ve tried:

class MyDate
md=MyDate.new()
md.display
def display
puts “hi”
end
end

but failed… also with md=MyDate.new if () matters at all…
thanks

Robert D. wrote:

class MyDate
md=MyDate.new()
md.display
def display
puts “hi”
end
end
look at David’s original post again

Remember that ruby is interpreted, not compiled. If the interpreter
hasn’t textually seen a method definition yet, it does not exist.
Add to that the fact that display is a base method in ruby to display
prints the contents of an object here’s what happens:

md = MyDate.new() makes a new object of type MyDate
md.display Invokes the Object.display method… but since MyDate has
no members, there’s not much to display

def display … overrides the Object.display method finally…

Now if you had written:

class MyDate
def display
puts “hi”
end
md=MyDate.new()
md.display
end

Things might be different. Whether you get what you want? That depends
a lot on what you want.

Ron

Jay P. wrote:

this.name=name;
{

mc.setName(“James”); //calls method with the instance of the class
j=myclass.i; //here coz i belongs to class.
}

Hope, it clears out what I’m trying to do. Just example, not very good
in java either…
So, what I’m trying to do is declare similar method as printMessage() in
ruby. thanks

A mantra for you… Ruby is not Java repeat 100 times until you
believe it. Then start learning Ruby all over again with a beginner’s
mind. Java is compiled, Ruby interpreted. Ruby knows nothing of your
display method until it sees it… see my previous post.
R