Today I learned we can use ls
to...
Show methods, constants, and variables. Ruby 3.2 Official Documentation IRB Commands
Examples
Looking at a class Greeting
I created:
?> class Greeting ?> attr_accessor :name ?> ?> def initialize(name) ?> self.name = name ?> end ?> ?> def to_s ?> 'Hello, %s' % name ?> end >> end => :to_s >> ls Greeting Greeting#methods: name name= speak to_s >> ls Greeting.new('foo') Greeting#methods: name name= speak to_s instance variables: @name
Looking at a built-in Ruby class, Struct
>> ls Struct Struct.methods: new Struct#methods: == [] []= deconstruct deconstruct_keys dig each each_pair eql? filter hash inspect length members pretty_print pretty_print_cycle select size to_a to_h to_s values values_at Enumerable#methods: all? any? chain chunk chunk_while collect collect_concat compact count cycle detect drop drop_while each_cons each_entry each_slice each_with_index each_with_object entries filter_map find find_all find_index first flat_map grep grep_v group_by include? inject lazy map max max_by member? min min_by minmax minmax_by none? one? partition reduce reject reverse_each slice_after slice_before slice_when sort sort_by sum take take_while tally to_set uniq zip
Top comments (2)
Thanks for this! (I wrote the page you linked to, but have not used -- or even been much aware of -- many of the commands.)
Should add: this beats the tar our of
Foo.methods
, etc.Oh, very cool and thanks!
Definitely! And I like that we have a couple options. Sometimes just helps to see things from a different angle. Always fun to make new discoveries too. :D