Yu-Chieh’s Blog (Y.C. Chang)

Ruby on Rails / Rubygems / FullStack / Git / Mac notes.

How to Get a List of a Class or Instance Methods by Keyword

a very useful tip to find methods in ruby/rails console

1
2
3
4
5
6
7
8
# Class.methods.grep(/keyword/) 

# e.g. 
irb> 1.methods.grep(/to/) #  => [:to_s, :to_f, :to_default_s, :to_json, :upto, :downto, :to_i, :to_int, :numerator, :denominator, :to_r, :to_bn, :to_d, :singleton_method_added, :to_c, :to_formatted_s, :to_param, :to_query, :psych_to_yaml, :to_yaml, :to_yaml_properties, :singleton_class, :singleton_methods, :respond_to?, :singleton_method, :define_singleton_method, :to_enum]

# Class.instance_methods.grep(/method/) #  => [:instance_methods, :public_instance_methods, :protected_instance_methods, :private_instance_methods, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods]

# Class.methods.grep(/new/) # => ["new"]

Comments