Re: function like "function_exits"



On Dec 7, 8:40 am, Robert Dober <robert.do...@xxxxxxxxx> wrote:
On Dec 7, 2007 2:49 PM, MonkeeSage <MonkeeS...@xxxxxxxxx> wrote:



On Dec 7, 7:31 am, Girard Fred <fred.gir...@xxxxxxxxx> wrote:
Jordan Callicoat wrote:
On Dec 7, 5:33 am, Alex Young <a...@xxxxxxxxxxxxxxx> 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 :)
--
Posted viahttp://www.ruby-forum.com/.

But you don't really want that... ;)

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.

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

You have to pass a second parameter to respond_to? to include private
methods. ;)

Regards,
Jordan
.



Relevant Pages

  • Re: Ruby reflection - get method definition as a string
    ... the actual test to the console or store it to a file. ... I know ruby supports a lot of reflection (like .methods ... puts line if flag ...
    (comp.lang.ruby)
  • Re: Top-level
    ... The question's about top-level methods: ... Let's take puts as an example. ... so why is it able to call private methods of ... And since "main" is an instance of Object or one of its subclasses it has method "puts". ...
    (comp.lang.ruby)
  • Re: How to adopt "Python Style" indentation for Ruby
    ... shows a partial solution to your challenge by overloading require. ... All of these ":" solutions would have potential incompatibilities with ...
    (comp.lang.ruby)
  • Re: for or each?
    ... puts "#objects" ... See Ruby patches for one that I ...
    (comp.lang.ruby)
  • Re: finalizer not getting called
    ... puts 'exiting' ...
    (comp.lang.ruby)