Posted about 7 years ago. Visible to the public. Repeats.

Finding a method name on a Ruby object

Wondering how a specific method on an object is exactly named? You can use Enumerable#grep to detect it in the array of methods.

Copy
@user.methods.grep /name/ # => [:name, :first_name, :last_name]

You can also call #private_methods or #public_methods. To find only relevant methods, it is suggested to subtract generic methods like this:

Copy
User.methods - Object.methods User.methods - ActiveRecord::Base.methods
Copy
@user.methods - Object.instance_methods @user.methods - ActiveRecord::Base.instance_methods

Once an application no longer requires constant development, it needs periodic maintenance for stable and secure operation. makandra offers monthly maintenance contracts that let you focus on your business while we make sure the lights stay on.

Owner of this card:

Avatar
Thomas Klemm
Last edit:
about 4 years ago
by Arne Hartherz
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Thomas Klemm to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more