Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby metaprogramming - getting method names and parameter info for a class

I want to get class methods in an object. Please see the following example I have a class "user.rb"

class User
  def say_name

  end

  def walk(p1)

  end

  def run(p1, p2)

  end
end

and I wrote the following code

require 'user.rb'

a = User.new

arr = a.public_methods(all = false)

Above code will return the method name, But my question is I want to get the method name with parameters

def def run(p1, p2)

end

I want to get the method name ("run") and its parameter names (p1, p2) or parameter count (2)

can someone help me, thanks in advance

cheers

sameera

like image 298
sameera207 Avatar asked Sep 24 '26 02:09

sameera207


1 Answers

User.new.method(:run).arity   # => 2
like image 58
Gishu Avatar answered Sep 27 '26 00:09

Gishu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!