Function like "function_exits"

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

On Dec 6, 2007 4:37 PM, Girard F. [email protected] 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
© & (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.)

Robert D. wrote:

On Dec 6, 2007 4:37 PM, Girard F. [email protected] 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
© & (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, 10:11 am, Lee J. [email protected] wrote:

Lee

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

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

Regards,
Jordan

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

This is a method…

Robert D.'s options are the best way to find what you’re looking for

Regards,
Lee

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

Peter B. 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

Alex Y. wrote:

Girard F. 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

Girard F. 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.

On Dec 7, 2007 8:35 AM, Peter B. [email protected] wrote:

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

Girard F. 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 W.

On Dec 7, 4:27 am, Robert D. [email protected] wrote:

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

Robert D. wrote:

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:

[email protected]:~$ cat toplevel.rb
def foo
puts “Foo is being called”
end
p methods.include?(“foo”)

[email protected]:~$ ruby toplevel.rb
false

On Dec 7, 2007 12:33 PM, Alex Y. [email protected] wrote:

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

Alex

IRB is not a good tool to test these behaviors as the toplevel object
is not the same.
Test it in a program.
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.)

Jordan Callicoat wrote:

On Dec 7, 5:33 am, Alex Y. [email protected] 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:

On Dec 7, 7:31 am, Girard F. [email protected] 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

On Dec 7, 5:33 am, Alex Y. [email protected] 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

Jordan Callicoat wrote:

On Dec 7, 7:31 am, Girard F. [email protected] 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, 8:16 am, Girard F. [email protected] wrote:

on your code), but usually there are better ways of doing things (like
end

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

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:40 am, Robert D. [email protected]in.invalid wrote:

Toplevel methods are implicitly added as private methods of class

end
puts methods.include?(‘foo’)

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

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

You have to pass a second parameter to respond_to? to include private
methods. :wink:

Regards,
Jordan

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs