In Files

  • mrbgems/mruby-struct/mrblib/struct.rb
  • mrbgems/mruby-struct/test/struct.rb

Struct

Public Instance Methods

_inspect(recur_list) click to toggle source
 
               # File mrbgems/mruby-struct/mrblib/struct.rb, line 49
def _inspect(recur_list)
  return "#<struct #{self.class}:...>" if recur_list[self.object_id]
  recur_list[self.object_id] = true
  name = self.class.to_s
  if name[0] == "#"
    str = "#<struct "
  else
    str = "#<struct #{name} "
  end
  buf = []
  self.each_pair do |k,v|
    buf.push [k.to_s + "=" + v._inspect(recur_list)]
  end
  str + buf.join(", ") + ">"
end
            
dig(key,...) → object click to toggle source

Extracts the nested value specified by the sequence of key objects by calling dig at each step, returning nil if any intermediate step is nil.

 
               # File mrbgems/mruby-struct/mrblib/struct.rb, line 91
def dig(idx,*args)
  n = self[idx]
  if args.size > 0
    n&.dig(*args)
  else
    n
  end
end
            
each(&block) click to toggle source

Calls the given block for each element of self and pass the respective element.

ISO 15.2.18.4.4

 
               # File mrbgems/mruby-struct/mrblib/struct.rb, line 14
def each(&block)
  self.class.members.each{|field|
    block.call(self[field])
  }
  self
end
            
each_pair(&block) click to toggle source

Calls the given block for each element of self and pass the name and value of the respectiev element.

ISO 15.2.18.4.5

 
               # File mrbgems/mruby-struct/mrblib/struct.rb, line 27
def each_pair(&block)
  self.class.members.each{|field|
    block.call(field.to_sym, self[field])
  }
  self
end
            
to_s → string click to toggle source
inspect → string

Describe the contents of this struct in a string.

15.2.18.4.10(x)

 
               # File mrbgems/mruby-struct/mrblib/struct.rb, line 74
def inspect
  self._inspect({})
end
            
Also aliased as: to_s
select(&block) click to toggle source

Calls the given block for each element of self and returns an array with all elements of which block is not false.

ISO 15.2.18.4.7

 
               # File mrbgems/mruby-struct/mrblib/struct.rb, line 40
def select(&block)
  ary = []
  self.class.members.each{|field|
    val = self[field]
    ary.push(val) if block.call(val)
  }
  ary
end
            
to_s() click to toggle source

15.2.18.4.11(x)

Alias for: inspect