3

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

3 Answers 3

5
User.new.method(:run).arity   # => 2
Sign up to request clarification or add additional context in comments.

1 Comment

or, if you dont want to create an instance: User.instance_method(:run).arity #=> 2
1

if you want parameters then http://github.com/rdp/arguments is your friend

Comments

1

You want:

User.new.method(:run).parameters # => [[:req, :p1], [:req, :p2]]

req means it's a required field. Other values you might get are:

  • def run(p1 = nil) => [[:opt, :p1]]
  • def run(*p1) => [[:rest, :p1]]
  • def run(&p1) => [[:block, :p1]]
  • def run(p1:) => [[:key, :p1]]
  • def run(p1: nil) => [[:keyopt, :p1]]
  • def run(**p1) => [[:keyrest, :p1]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.