`ls` to inspect object



This content originally appeared on DEV Community and was authored by Kelly Popko

Today I learned we can use ls to…

Show methods, constants, and variables. -g [query] or -G [query] allows you to filter out the output. Ruby 3.2 Official Documentation

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_sG
>> 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

Sources


This content originally appeared on DEV Community and was authored by Kelly Popko