Class: Struct

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
mrbgems/mruby-struct/mrblib/struct.rb

Overview

Struct

ISO 15.2.18

Constant Summary

Constants included from Enumerable

Enumerable::NONE

Instance Method Summary collapse

Methods included from Enumerable

#all?, #any?, #chain, #chunk, #chunk_while, #collect, #count, #cycle, #detect, #drop, #drop_while, #each_cons, #each_entry, #each_slice, #each_with_index, #each_with_object, #entries, #filter_map, #find_all, #find_index, #first, #flat_map, #grep, #grep_v, #group_by, #hash, #include?, #inject, #lazy, #max, #max_by, #min, #min_by, #minmax, #minmax_by, #none?, #one?, #partition, #reject, #reverse_each, #sort, #sort_by, #sum, #take, #take_while, #tally, #to_h, #uniq, #zip

Instance Method Details

#dig(idx, *args) ⇒ Object

call-seq: hsh.dig(key,…) -> object

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.



61
62
63
64
65
66
67
68
# File 'mrbgems/mruby-struct/mrblib/struct.rb', line 61

def dig(idx,*args)
  n = self[idx]
  if args.size > 0
    n&.dig(*args)
  else
    n
  end
end

#each(&block) ⇒ Object

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

ISO 15.2.18.4.4



13
14
15
16
17
18
# File 'mrbgems/mruby-struct/mrblib/struct.rb', line 13

def each(&block)
  self.class.members.each{|field|
    block.call(self[field])
  }
  self
end

#each_pair(&block) ⇒ Object

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

ISO 15.2.18.4.5



26
27
28
29
30
31
# File 'mrbgems/mruby-struct/mrblib/struct.rb', line 26

def each_pair(&block)
  self.class.members.each{|field|
    block.call(field.to_sym, self[field])
  }
  self
end

#select(&block) ⇒ Object

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



39
40
41
42
43
44
45
46
# File 'mrbgems/mruby-struct/mrblib/struct.rb', line 39

def select(&block)
  ary = []
  self.class.members.each{|field|
    val = self[field]
    ary.push(val) if block.call(val)
  }
  ary
end