From 0f8d3d8777e9a29a557ae95b93d3a680ef8ae775 Mon Sep 17 00:00:00 2001 From: dearblue Date: Sat, 6 Feb 2021 18:05:05 +0900 Subject: No need to check class definition Because now the `Struct` class is always defined when this file is included. --- mrbgems/mruby-struct/mrblib/struct.rb | 165 +++++++++++++++++----------------- 1 file changed, 81 insertions(+), 84 deletions(-) diff --git a/mrbgems/mruby-struct/mrblib/struct.rb b/mrbgems/mruby-struct/mrblib/struct.rb index b398409c3..2439e2a37 100644 --- a/mrbgems/mruby-struct/mrblib/struct.rb +++ b/mrbgems/mruby-struct/mrblib/struct.rb @@ -2,99 +2,96 @@ # Struct # # ISO 15.2.18 +class Struct -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 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 respective + # 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 pass the name and value of the respective - # 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 - ## - # 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 + def _inspect(recur_list) + return "#" if recur_list[self.object_id] + recur_list[self.object_id] = true + name = self.class.to_s + if name[0] == "#" + str = "#" if recur_list[self.object_id] - recur_list[self.object_id] = true - name = self.class.to_s - if name[0] == "#" - str = "#" + buf = [] + self.each_pair do |k,v| + buf.push k.to_s + "=" + v._inspect(recur_list) 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 - self._inspect({}) - 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 + self._inspect({}) + end - ## - # 15.2.18.4.11(x) - # - alias to_s inspect + ## + # 15.2.18.4.11(x) + # + alias to_s inspect - ## - # 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+. - # - def dig(idx,*args) - n = self[idx] - if args.size > 0 - n&.dig(*args) - else - n - end + ## + # 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+. + # + def dig(idx,*args) + n = self[idx] + if args.size > 0 + n&.dig(*args) + else + n end end end -- cgit v1.2.3