Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Ruby - "meta method" execute (See related posts)

//
//
// attempt to execute a method indirectly, I don't know if
// it's possible, but I suspect it is, and probably just
// have the syntax wrong
//

ar = %w(apples bananas oranges)

print "\n ar.class = #{ar.class}"

print "\n ar.methods = #{ar.methods.sort}"
puts "======================================================="

mets = ar.methods.sort

#
# this doesn't work ... is there a syntax for doing this?
#                       i.e. calling a 'meta' method ?
#
# call each method of 'ar' (an array)
#
#

mets.each {|method| ar.method} 


Comments on this post

JonEvans posts on Aug 23, 2007 at 16:06
mets.each {|method| ar.send(method)} 
mstram posts on Aug 23, 2007 at 19:01
Thanks Jon,

The ruby help file says that I can trap exceptions with
rescue


,and says that it can be passed a parameter of which class of error to trap.

Which class is generating the 'no block given' error ?
i.e. what parameter do I give to 'rescue' to specifically handle that error?

Mike
Shadowfiend posts on Aug 24, 2007 at 00:41
Or ar.method(method_name).call (#send is better, though).

The no block given error is a LocalJumpError.
mstram posts on Aug 24, 2007 at 02:25
>Or ar.method(method_name).call (#send is better, though).

Why ?

>The no block given error is a LocalJumpError.

Where do you know that from ? :)

The main reference I'm using right now is the help file / book "Programming Ruby - The Pragmatic Programmer's Guide".

Is there something better online?

Mike
JonEvans posts on Aug 24, 2007 at 08:48
Well, I'm not really sure what you're trying to do. :)

You create a hash, then get an array of all of its method names. Then you call each one in turn. Some of those methods require arguments, and some of them require a block to be given. Each time you call a method though, you are passing no arguments to it.

You could do something like this:

mets.each do |method|
  begin
    ar.send(method)
  rescue Exception=>ex
    puts "Couldn't call #{method}, got #{ex.class.name}: #{ex.message}"
    # N.B. ex.message is also automatically put into the $! variable
  end
end


In an irb session, this gives me the following output:

Couldn't call &, got ArgumentError: wrong number of arguments (0 for 1)
Couldn't call *, got ArgumentError: wrong number of arguments (0 for 1)
Couldn't call +, got ArgumentError: wrong number of arguments (0 for 1)
Couldn't call -, got ArgumentError: wrong number of arguments (0 for 1)
Couldn't call <<, got ArgumentError: wrong number of arguments (0 for 1)
Couldn't call <=>, got ArgumentError: wrong number of arguments (0 for 1)
Couldn't call ==, got ArgumentError: wrong number of arguments (0 for 1)
Couldn't call ===, got ArgumentError: wrong number of arguments (0 for 1)
Couldn't call =~, got ArgumentError: wrong number of arguments (0 for 1)
Couldn't call [], got ArgumentError: wrong number of arguments (0 for 1)
Couldn't call []=, got ArgumentError: wrong number of arguments (0 for 2)
Couldn't call __send__, got ArgumentError: no method name given
Couldn't call assoc, got ArgumentError: wrong number of arguments (0 for 1)
Couldn't call at, got ArgumentError: wrong number of arguments (0 for 1)
Couldn't call clear, got TypeError: can't modify frozen array
Couldn't call collect!, got TypeError: can't modify frozen array
Couldn't call compact!, got TypeError: can't modify frozen array
Couldn't call concat, got ArgumentError: wrong number of arguments (0 for 1)
Couldn't call delete, got ArgumentError: wrong number of arguments (0 for 1)
Couldn't call delete_at, got ArgumentError: wrong number of arguments (0 for 1)
Couldn't call delete_if, got TypeError: can't modify frozen array
Couldn't call each_with_index, got LocalJumpError: no block given
Couldn't call eql?, got ArgumentError: wrong number of arguments (0 for 1)
Couldn't call equal?, got ArgumentError: wrong number of arguments (0 for 1)
Couldn't call extend, got ArgumentError: wrong number of arguments (0 for 1)
Couldn't call fetch, got ArgumentError: wrong number of arguments (0 for 1)
Couldn't call fill, got ArgumentError: wrong number of arguments (0 for 1)
Couldn't call gem, got ArgumentError: wrong number of arguments (0 for 1)
Couldn't call grep, got ArgumentError: wrong number of arguments (0 for 1)
(irb):26: warning: Object#id will be deprecated; use Object#object_id
Couldn't call include?, got ArgumentError: wrong number of arguments (0 for 1)
Couldn't call index, got ArgumentError: wrong number of arguments (0 for 1)
(irb):26: warning: Array#indexes is deprecated; use Array#values_at
(irb):26: warning: Array#indices is deprecated; use Array#values_at
Couldn't call insert, got ArgumentError: wrong number of arguments (at least 1)
Couldn't call instance_eval, got ArgumentError: block not supplied
Couldn't call instance_of?, got ArgumentError: wrong number of arguments (0 for 1)
Couldn't call instance_variable_defined?, got ArgumentError: wrong number of arguments (0 for 1)
Couldn't call instance_variable_get, got ArgumentError: wrong number of arguments (0 for 1)
Couldn't call instance_variable_set, got ArgumentError: wrong number of arguments (0 for 2)
Couldn't call is_a?, got ArgumentError: wrong number of arguments (0 for 1)
Couldn't call kind_of?, got ArgumentError: wrong number of arguments (0 for 1)
Couldn't call map!, got TypeError: can't modify frozen array
Couldn't call member?, got ArgumentError: wrong number of arguments (0 for 1)
Couldn't call method, got ArgumentError: wrong number of arguments (0 for 1)
Couldn't call pack, got ArgumentError: wrong number of arguments (0 for 1)
Couldn't call pop, got TypeError: can't modify frozen array
Couldn't call rassoc, got ArgumentError: wrong number of arguments (0 for 1)
Couldn't call reject!, got TypeError: can't modify frozen array
Couldn't call replace, got ArgumentError: wrong number of arguments (0 for 1)
Couldn't call require, got ArgumentError: wrong number of arguments (0 for 1)
Couldn't call require_gem, got ArgumentError: wrong number of arguments (0 for 1)
Couldn't call respond_to?, got ArgumentError: wrong number of arguments (0 for 1)
Couldn't call reverse!, got TypeError: can't modify frozen array
Couldn't call rindex, got ArgumentError: wrong number of arguments (0 for 1)
Couldn't call send, got ArgumentError: no method name given
Couldn't call shift, got TypeError: can't modify frozen array
Couldn't call slice, got ArgumentError: wrong number of arguments (0 for 1)
Couldn't call slice!, got ArgumentError: wrong number of arguments (0 for 1)
Couldn't call sort!, got TypeError: can't modify frozen array
Couldn't call taguri=, got ArgumentError: wrong number of arguments (0 for 1)
Couldn't call taint, got TypeError: can't modify frozen object
(irb):26: warning: Object#type is deprecated; use Object#class
Couldn't call yaml_initialize, got ArgumentError: wrong number of arguments (0 for 2)
Couldn't call |, got ArgumentError: wrong number of arguments (0 for 1)
mstram posts on Aug 24, 2007 at 12:57
Jon,

I'm just trying to do some code "spelunking" ;)

Thanks !

 rescue Exception => ex
    puts "Couldn't call #{method}, got #{ex.class.name}: #{ex.message}"


This code is VERY helpful !

I didn't know about the 'ex.class.name'

What does the
 Exception => ex 
idiom mean?

Is 'ex' being *created* by Exception ? (That's what it looks like to me)

Mike

You need to create an account or log in to post comments to this site.


Click here to browse all 5147 code snippets

Related Posts