define and call methods in ruby

J

Jay Pangmi

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

David A. Black

Hi --

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

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

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
Advancing with Rails January 19-22 Fort Lauderdale, FL *
* Co-taught with Patrick Ewing!
See http://www.rubypal.com for details and updates!
 
J

Jay Pangmi

David said:
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...
 
R

Robert Klemme

2008/9/11 Jay Pangmi said:
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?

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

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
 
D

David A. Black

Hi --

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
================================================================================

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

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
Advancing with Rails January 19-22 Fort Lauderdale, FL *
* Co-taught with Patrick Ewing!
See http://www.rubypal.com for details and updates!
 
J

Jay Pangmi

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
 
J

Jay Pangmi

David said:
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
 
R

Robert Dober

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

class MyDate
md=MyDate.new()
md.display
def display
puts "hi"
end
end
look at David's original post *again*
 
R

Ron Fox

Robert said:
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
 
R

Ron Fox

Jay said:
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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,273
Latest member
DamonShoem

Latest Threads

Top