Function like "function_exits"

Hi all,
I'm new dev in Ruby, and my first question here:
how to know if funtion exists ?

···

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

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

···

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-forum.com/\.

--

http://ruby-smalltalk.blogspot.com/

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

Robert Dober 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-forum.com/\.

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.blogspot.com/

---
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')

:wink:

···

On Dec 6, 2007 4:37 PM, Girard Fred <fred.girard@gmail.com> wrote:

--
Posted via http://www.ruby-forum.com/\.

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-forum.com/\.

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-forum.com/\.

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

Regards,
Jordan

···

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-forum.com/.

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 ?:wink:

Anyway thanks

···

--
Posted via http://www.ruby-forum.com/\.

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.

···

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

--

http://ruby-smalltalk.blogspot.com/

---
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 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 ?:wink:

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

> > 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.blogspot.com/

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

Works here (even though respond_to? is misspelled :wink:

def foo; end

=> nil

methods.include? "foo"

=> true

Regards,
Jordan

···

On Dec 7, 4:27 am, Robert Dober <robert.do...@gmail.com> wrote:

On Dec 7, 2007 8:35 AM, Peter Bunyan <peter.bun...@gmail.com> wrote:

Robert Dober 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?

They're included in IRB:

irb(main):001:0> def foo
irb(main):002:1> puts "Foo is being called"
irb(main):003:1> end
=> nil
irb(main):004:0> methods.include? "foo"
=> true

but not in a script:

alex@shaxam:~$ cat toplevel.rb
def foo
   puts "Foo is being called"
end
p methods.include?("foo")

alex@shaxam:~$ ruby toplevel.rb
false

···

On Dec 7, 2007 8:35 AM, Peter Bunyan <peter.bunyan@gmail.com> wrote:

--
Alex

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 ?:wink:

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-forum.com/\.

IRB is not a good tool to test these behaviors as the toplevel object
is not the same.
Test it in a program.
R.

···

On Dec 7, 2007 12:33 PM, Alex Young <alex@blackkettle.org> wrote:

Robert Dober wrote:
> 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?

They're included in IRB:

irb(main):001:0> def foo
irb(main):002:1> puts "Foo is being called"
irb(main):003:1> end
=> nil
irb(main):004:0> methods.include? "foo"
=> true

but not in a script:

alex@shaxam:~$ cat toplevel.rb
def foo
   puts "Foo is being called"
end
p methods.include?("foo")

alex@shaxam:~$ ruby toplevel.rb
false

--
Alex

--

http://ruby-smalltalk.blogspot.com/

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

Toplevel methods are implicitly added as private methods of class
Object, so...

p self.private_methods.include?("foo") # => true

Regards,
Jordan

···

On Dec 7, 5:33 am, Alex Young <a...@blackkettle.org> wrote:

Robert Dober wrote:
> On Dec 7, 2007 8:35 AM, Peter Bunyan <peter.bun...@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?

They're included in IRB:

irb(main):001:0> def foo
irb(main):002:1> puts "Foo is being called"
irb(main):003:1> end
=> nil
irb(main):004:0> methods.include? "foo"
=> true

but not in a script:

alex@shaxam:~$ cat toplevel.rb
def foo
   puts "Foo is being called"
end
p methods.include?("foo")

alex@shaxam:~$ ruby toplevel.rb
false

--
Alex

Jordan Callicoat wrote:

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

Alex

Toplevel methods are implicitly added as private methods of class
Object, so...

p self.private_methods.include?("foo") # => true

Regards,
Jordan

It's true, i try this differents ways :
in irb:

def foo
puts 'test'
end

=> nil

methods.include? "foo

=> true

···

On Dec 7, 5:33 am, Alex Young <a...@blackkettle.org> wrote:

--------------------
in test file:
def foo()
  puts 'test'
end

puts methods.include?('foo')

=> false
---------------------
So, now with your private_methods

def foo()
  puts 'test'
end

puts private_methods.include?('foo')

=> true

Good job :slight_smile:
--
Posted via http://www.ruby-forum.com/\.

But you don't really want that... :wink:

You want respond_to? which Just Works everywhere. And really, you
probably don't even want to be asking if a method is defined (depends
on your code), but usually there are better ways of doing things (like
unit testing).

HTH,
Jordan

···

On Dec 7, 7:31 am, Girard Fred <fred.gir...@gmail.com> wrote:

Jordan Callicoat wrote:
> On Dec 7, 5:33 am, Alex Young <a...@blackkettle.org> wrote:
>> >> A method and a function are the same thing. Also, check this out.

>> Alex
> Toplevel methods are implicitly added as private methods of class
> Object, so...

> p self.private_methods.include?("foo") # => true

> Regards,
> Jordan

It's true, i try this differents ways :
in irb:>> def foo
>> puts 'test'
>> end
=> nil
>> methods.include? "foo

=> true
--------------------
in test file:
def foo()
  puts 'test'
end

puts methods.include?('foo')

=> false
---------------------
So, now with your private_methods

def foo()
  puts 'test'
end

puts private_methods.include?('foo')

=> true

Good job :slight_smile:
--
Posted viahttp://www.ruby-forum.com/.

Jordan Callicoat wrote:

> Regards,
--------------------

Posted viahttp://www.ruby-forum.com/.

But you don't really want that... :wink:

You want respond_to? which Just Works everywhere. And really, you
probably don't even want to be asking if a method is defined (depends
on your code), but usually there are better ways of doing things (like
unit testing).

HTH,
Jordan

No it's ok, i am happy with that, if i really want, i can merge these 2
methods:

def foo()
   puts 'test'
end

def function?(function_name)
   return true if private_methods.include?(function_name)
   return true if methods.include?(function_name)
   return false
end

puts function?('foo') => true
puts function?('notfoo') => false
puts function?('puts') => true

It's nice to learn a new language like that, thank you so much (and
sorry for my poor english)
:slight_smile:

···

On Dec 7, 7:31 am, Girard Fred <fred.gir...@gmail.com> wrote:

--
Posted via http://www.ruby-forum.com/\.

> Jordan Callicoat wrote:
> >> >> A method and a function are the same thing. Also, check this out.
>
> >> Alex
> > Toplevel methods are implicitly added as private methods of class
> > Object, so...
>
> > p self.private_methods.include?("foo") # => true
>
> > Regards,
> > Jordan
>
> It's true, i try this differents ways :
> in irb:>> def foo
> >> puts 'test'
> >> end
> => nil
> >> methods.include? "foo
>
> => true
> --------------------
> in test file:
> def foo()
> puts 'test'
> end
>
> puts methods.include?('foo')
>
> => false
> ---------------------
> So, now with your private_methods
>
> def foo()
> puts 'test'
> end
>
> puts private_methods.include?('foo')
>
> => true
>
> Good job :slight_smile:
> --
> Posted viahttp://www.ruby-forum.com/.

But you don't really want that... :wink:

You want respond_to? which Just Works everywhere.

No, it behaves the same, you will get a respond_to?(foo) false at the
main level.

···

On Dec 7, 2007 2:49 PM, MonkeeSage <MonkeeSage@gmail.com> wrote:

On Dec 7, 7:31 am, Girard Fred <fred.gir...@gmail.com> wrote:
> > On Dec 7, 5:33 am, Alex Young <a...@blackkettle.org> wrote:
And really, you
probably don't even want to be asking if a method is defined (depends
on your code), but usually there are better ways of doing things (like
unit testing).

HTH,
Jordan

--

http://ruby-smalltalk.blogspot.com/

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

Your english is fine. :slight_smile:

And I'm glad you like ruby!

But really, you want to use respond_to? ...that's why it was added to
the language:

http://www.ruby-doc.org/core/classes/Object.html#M000333

In rbuy, everything is an object, and every callable object is a
method. What it means when you say...

foo()

...is really this...

send(:foo) # actual ruby, try it!

In other words, you don't "call" a method in ruby, you send an object
a message that tells it to execute some code by that name. So, for
example, when you say...

[3,2,1].sort

...it really means...

[3,2,1].send(:sort) # try it!

So the way to check if an object can execute code with a certain name
("foo"), is to ask does it "respond_to?('foo')"...that means "can I
send the object with a message 'foo' and it knows how to execute it?"

If I don't make sense, just ignore me. :wink:

Regards,
Jordan

···

On Dec 7, 8:16 am, Girard Fred <fred.gir...@gmail.com> wrote:

Jordan Callicoat wrote:
> On Dec 7, 7:31 am, Girard Fred <fred.gir...@gmail.com> wrote:
>> > Regards,
>> --------------------

>> Posted viahttp://www.ruby-forum.com/.
> But you don't really want that... :wink:

> You want respond_to? which Just Works everywhere. And really, you
> probably don't even want to be asking if a method is defined (depends
> on your code), but usually there are better ways of doing things (like
> unit testing).

> HTH,
> Jordan

No it's ok, i am happy with that, if i really want, i can merge these 2
methods:

def foo()
   puts 'test'
end

def function?(function_name)
   return true if private_methods.include?(function_name)
   return true if methods.include?(function_name)
   return false
end

puts function?('foo') => true
puts function?('notfoo') => false
puts function?('puts') => true

It's nice to learn a new language like that, thank you so much (and
sorry for my poor english)
:slight_smile:

--
Posted viahttp://www.ruby-forum.com/.