DEV Community

Kelly Popko
Kelly Popko

Posted on • Edited on

IRB and `ls` to inspect object

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 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

Sources

Top comments (2)

Collapse
 
burdettelamar profile image
Burdette Lamar • Edited

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.

Collapse
 
kelpopko profile image
Kelly Popko

Oh, very cool and thanks!

this beats the tar our of Foo.methods, etc.

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