Sha256: f9bb12eac1c4b22b5e5c6128d9edf3da6862816f3201b977d237f20196c5cad1

Contents?: true

Size: 1.96 KB

Versions: 23

Compression:

Stored size: 1.96 KB

Contents

##
# Struct
#
# ISO 15.2.18

if Object.const_defined?(:Struct)
  class Struct

    ##
    # Calls the given block for each element of +self+
    # and pass the respective element.
    #
    # ISO 15.2.18.4.4
    def each(&block)
      self.class.members.each{|field|
        block.call(self[field])
      }
      self
    end

    ##
    # 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
    def each_pair(&block)
      self.class.members.each{|field|
        block.call(field.to_sym, self[field])
      }
      self
    end

    ##
    # 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
    def select(&block)
      ary = []
      self.class.members.each{|field|
        val = self[field]
        ary.push(val) if block.call(val)
      }
      ary
    end

    def _inspect
      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]
      end
      str + buf.join(", ") + ">"
    end

    ##
    # call-seq:
    #   struct.to_s      -> string
    #   struct.inspect   -> string
    #
    # Describe the contents of this struct in a string.
    #
    # 15.2.18.4.10(x)
    #
    def inspect
      begin
        self._inspect
      rescue SystemStackError
        "#<struct #{self.class.to_s}:...>"
      end
    end

    ##
    # 15.2.18.4.11(x)
    #
    alias to_s inspect
  end

  ##
  # call-seq:
  #   hsh.dig(key,...)                 -> object
  #
  # Extracts the nested value specified by the sequence of <i>key</i>
  # objects by calling +dig+ at each step, returning +nil+ if any
  # intermediate step is +nil+.
  #
  def dig(idx,*args)
    n = self[idx]
    if args.size > 0
      n&.dig(*args)
    else
      n
    end
  end
end

Version data entries

23 entries across 23 versions & 2 rubygems

Version Path
script_core-0.0.6 ext/enterprise_script_service/mruby/mrbgems/mruby-struct/mrblib/struct.rb
script_core-0.0.5 ext/enterprise_script_service/mruby/mrbgems/mruby-struct/mrblib/struct.rb
esruby-0.2.0 resources/mruby/mrbgems/mruby-struct/mrblib/struct.rb
esruby-0.1.5 resources/mruby/mrbgems/mruby-struct/mrblib/struct.rb
esruby-0.1.4 resources/mruby/mrbgems/mruby-struct/mrblib/struct.rb
esruby-0.1.3 resources/mruby/mrbgems/mruby-struct/mrblib/struct.rb
esruby-0.1.2 resources/mruby/mrbgems/mruby-struct/mrblib/struct.rb
esruby-0.1.1 resources/mruby/mrbgems/mruby-struct/mrblib/struct.rb
esruby-0.1.0 resources/mruby/mrbgems/mruby-struct/mrblib/struct.rb
esruby-0.0.11 resources/mruby/mrbgems/mruby-struct/mrblib/struct.rb
esruby-0.0.10 resources/mruby/mrbgems/mruby-struct/mrblib/struct.rb
esruby-0.0.9 resources/mruby/mrbgems/mruby-struct/mrblib/struct.rb
esruby-0.0.8 resources/mruby/mrbgems/mruby-struct/mrblib/struct.rb
esruby-0.0.7 resources/mruby/mrbgems/mruby-struct/mrblib/struct.rb
esruby-0.0.6 resources/mruby/mrbgems/mruby-struct/mrblib/struct.rb
esruby-0.0.5 resources/mruby/mrbgems/mruby-struct/mrblib/struct.rb
esruby-0.0.3 resources/mruby/mrbgems/mruby-struct/mrblib/struct.rb
esruby-0.0.2 resources/mruby/mrbgems/mruby-struct/mrblib/struct.rb
esruby-0.0.0 resources/mruby/mrbgems/mruby-struct/mrblib/struct.rb
script_core-0.0.4 ext/enterprise_script_service/mruby/mrbgems/mruby-struct/mrblib/struct.rb