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
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.
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
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.
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
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.
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.