[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: define and call methods in ruby

Dan Diebolt

9/11/2008 1:12:00 PM

[Note: parts of this message were removed to make it a legal post.]

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
3 Answers

David A. Black

9/11/2008 1:38:00 PM

0

Hi --

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

> 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

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


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.r... for details and updates!

Jay Pangmi

9/11/2008 2:08:00 PM

0

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
--
Posted via http://www.ruby-....

Ron Fox

9/15/2008 10:46:00 AM

0

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

--
Ron Fox
NSCL
Michigan State University
East Lansing, MI 48824-1321