2

I have a method definition like this:

def m1(a:Int, b:String, c:Val)                                                 
end

m = method(:m1) 

Note that Int, String, Val are default values for a, b, c.

Is it possible to get this information from the method object m?

sawa
  • 165,429
  • 45
  • 277
  • 381
songyy
  • 4,323
  • 6
  • 41
  • 63
  • this topic may help you http://stackoverflow.com/questions/622324/getting-argument-names-in-ruby-reflection I tried by myself to get those values following Method(http://ruby-doc.org/core-2.2.0/Method.html#method-i-parameters) doc, but only what I got is whether argument is required or optional. – djaszczurowski May 30 '15 at 09:44
  • Do you want to get the types (classes) of the parameters in ruby? parameters cannot be bound to a type. More information: http://stackoverflow.com/questions/14575581/can-i-tell-a-ruby-method-to-expect-a-specific-parameter-type – Vince V. May 30 '15 at 11:27

1 Answers1

1

No, this is not possible. It has been discussed numerous times on the ruby-core mailinglist, but it's simply not feasible. What should your proposed method return for something like this:

def m(p: if rand < 0.5 then Time.now else ENV['HOME'] end) end
Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653