[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

function like "function_exits"

Girard Fred

12/6/2007 3:37:00 PM

Hi all,
I'm new dev in Ruby, and my first question here:
how to know if funtion exists ?
--
Posted via http://www.ruby-....

21 Answers

Robert Dober

12/6/2007 3:47:00 PM

0

On Dec 6, 2007 4:37 PM, Girard Fred <fred.girard@gmail.com> wrote:
> Hi all,
> I'm new dev in Ruby, and my first question here:
> how to know if funtion exists ?
> --
> Posted via http://www.ruby-....
>
>
that is quite simple, but is this really what you want to know?

(a) method exists for a given object
a.methods.include? "my_method"
(b) get method if it exists
a.method("my_method") rescue nil
(c) & (d) the same game can be played with instance_methods

HTH
Robert





--

http://ruby-smalltalk.blo...

---
All truth passes through three stages. First, it is ridiculed. Second,
it is violently opposed. Third, it is accepted as being self-evident.
Schopenhauer (attr.)

Girard Fred

12/6/2007 4:08:00 PM

0

Robert Dober wrote:
> On Dec 6, 2007 4:37 PM, Girard Fred <fred.girard@gmail.com> wrote:
>> Hi all,
>> I'm new dev in Ruby, and my first question here:
>> how to know if funtion exists ?
>> --
>> Posted via http://www.ruby-....
>>
>>
> that is quite simple, but is this really what you want to know?
>
> (a) method exists for a given object
> a.methods.include? "my_method"
> (b) get method if it exists
> a.method("my_method") rescue nil
> (c) & (d) the same game can be played with instance_methods
>
> HTH
> Robert
>
>
>
>
>
> --
>
> http://ruby-smalltalk.blo...
>
> ---
> All truth passes through three stages. First, it is ridiculed. Second,
> it is violently opposed. Third, it is accepted as being self-evident.
> Schopenhauer (attr.)

Thanks for your answer Robert.
It's not a method but a simple function like
def foo()
#some code
end

is there something like :
functions.exist?('foo')

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

Lee Jarvis

12/6/2007 4:11:00 PM

0

> It's not a method but a simple function like
> def foo()
> #some code
> end

This is a method..

Robert Dober's options are the best way to find what you're looking for

Regards,
Lee
--
Posted via http://www.ruby-....

MonkeeSage

12/7/2007 1:40:00 AM

0

On Dec 6, 10:11 am, Lee Jarvis <ljjar...@gmail.com> wrote:
> > It's not a method but a simple function like
> > def foo()
> > #some code
> > end
>
> This is a method..
>
> Robert Dober's options are the best way to find what you're looking for
>
> Regards,
> Lee
> --
> Posted viahttp://www.ruby-....

a.methods.include? is normally spelled a.respond_to?

Regards,
Jordan

Peter Bunyan

12/7/2007 7:36:00 AM

0

> Thanks for your answer Robert.
> It's not a method but a simple function like
> def foo()
> #some code
> end
>
> is there something like :
> functions.exist?('foo')

A method and a function are the same thing. Also, check this out.

def foo
puts "Foo is being called."
end

methods.include? "foo"
=> true

method("foo").call
"Foo is being called."
--
Posted via http://www.ruby-....

Girard Fred

12/7/2007 10:04:00 AM

0

Peter Bunyan wrote:
>> Thanks for your answer Robert.
>> It's not a method but a simple function like
>> def foo()
>> #some code
>> end
>>
>> is there something like :
>> functions.exist?('foo')
>
> A method and a function are the same thing. Also, check this out.
>
> def foo
> puts "Foo is being called."
> end
>
> methods.include? "foo"
> => true
>
> method("foo").call
> "Foo is being called."

Thanks a lot Peter, it's the answer i m looking for;)
Just one thing:
a method, for me, is 'function or procedure' from an object
a function (standalone one) is some instructions that returns a result
a procedure (standalone one) execute instructions without necessary
returns result

It wrong or old scool ?;)

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

Alex Young

12/7/2007 10:14:00 AM

0

Girard Fred wrote:
> Peter Bunyan wrote:
>>> Thanks for your answer Robert.
>>> It's not a method but a simple function like
>>> def foo()
>>> #some code
>>> end
>>>
>>> is there something like :
>>> functions.exist?('foo')
>> A method and a function are the same thing. Also, check this out.
>>
>> def foo
>> puts "Foo is being called."
>> end
>>
>> methods.include? "foo"
>> => true
>>
>> method("foo").call
>> "Foo is being called."
>
> Thanks a lot Peter, it's the answer i m looking for;)
> Just one thing:
> a method, for me, is 'function or procedure' from an object
> a function (standalone one) is some instructions that returns a result
> a procedure (standalone one) execute instructions without necessary
> returns result
>
> It wrong or old scool ?;)

In Ruby, it is not possible for a function to exist without being
attached to an object; all functions are methods. Also, every method
returns a value; all procedures are functions.

--
Alex

Girard Fred

12/7/2007 10:18:00 AM

0

Alex Young wrote:
> Girard Fred wrote:
>>>
>> Thanks a lot Peter, it's the answer i m looking for;)
>> Just one thing:
>> a method, for me, is 'function or procedure' from an object
>> a function (standalone one) is some instructions that returns a result
>> a procedure (standalone one) execute instructions without necessary
>> returns result
>>
>> It wrong or old scool ?;)
>
> In Ruby, it is not possible for a function to exist without being
> attached to an object; all functions are methods. Also, every method
> returns a value; all procedures are functions.

this explains that:)

Thank you all
--
Posted via http://www.ruby-....

Jari Williamsson

12/7/2007 10:27:00 AM

0

Girard Fred wrote:
> a method, for me, is 'function or procedure' from an object
> a function (standalone one) is some instructions that returns a result
> a procedure (standalone one) execute instructions without necessary
> returns result

---
def foo
p self.class
end

foo
---

...will result in "Object"


Best regards,

Jari Williamsson

Robert Dober

12/7/2007 10:28:00 AM

0

On Dec 7, 2007 8:35 AM, Peter Bunyan <peter.bunyan@gmail.com> wrote:
> > Thanks for your answer Robert.
> > It's not a method but a simple function like
> > def foo()
> > #some code
> > end
> >
> > is there something like :
> > functions.exist?('foo')
>
> A method and a function are the same thing. Also, check this out.
>
> def foo
> puts "Foo is being called."
> end
>
> methods.include? "foo"
> => true
you surely mean false here, right?
the methods method of the toplevel does not seem to include the user
defined methods, or is this just a bug?
R.
--

http://ruby-smalltalk.blo...

---
All truth passes through three stages. First, it is ridiculed. Second,
it is violently opposed. Third, it is accepted as being self-evident.
Schopenhauer (attr.)