[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

define and call methods in ruby

Jay Pangmi

9/11/2008 11:30:00 AM

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

7 Answers

David A. Black

9/11/2008 11:45:00 AM

0

Hi --

On Thu, 11 Sep 2008, Jay Pangmi wrote:

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

Jay Pangmi

9/11/2008 12:49:00 PM

0

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

Robert Klemme

9/11/2008 1:01:00 PM

0

2008/9/11 Jay Pangmi <jaeezzy@gmail.com>:
> 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

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

--
use.inject do |as, often| as.you_can - without end

David A. Black

9/11/2008 1:37:00 PM

0

Hi --

On Thu, 11 Sep 2008, Jay Pangmi wrote:

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

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

Jay Pangmi

9/11/2008 2:38:00 PM

0

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

--
Posted via http://www.ruby-....

Robert Dober

9/11/2008 2:48:00 PM

0

On Thu, Sep 11, 2008 at 4:37 PM, Jay Pangmi <jaeezzy@gmail.com> wrote:
> 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
look at David's original post *again*

Ron Fox

9/15/2008 10:44:00 AM

0

Robert Dober wrote:
> On Thu, Sep 11, 2008 at 4:37 PM, Jay Pangmi <jaeezzy@gmail.com> wrote:
>> 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
> 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
--
Ron Fox
NSCL
Michigan State University
East Lansing, MI 48824-1321