From cf588bd5cb094e6ee22d8d45c2005cf0da55d197 Mon Sep 17 00:00:00 2001 From: "go.kikuta" Date: Thu, 20 Aug 2015 17:28:34 +0900 Subject: range.rb: refactor code (use ! instead of not, use favor modifier if and unless usage when having a single-line body) --- mrblib/range.rb | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/mrblib/range.rb b/mrblib/range.rb index 64fa0cb6c..5e5fd9bdc 100644 --- a/mrblib/range.rb +++ b/mrblib/range.rb @@ -26,9 +26,7 @@ class Range return self end - unless val.respond_to? :succ - raise TypeError, "can't iterate" - end + raise TypeError, "can't iterate" unless val.respond_to? :succ return self if (val <=> last) > 0 @@ -37,18 +35,14 @@ class Range val = val.succ end - if not exclude_end? and (val <=> last) == 0 - block.call(val) - end + block.call(val) if !exclude_end? && (val <=> last) == 0 self end # redefine #hash 15.3.1.3.15 def hash h = first.hash ^ last.hash - if self.exclude_end? - h += 1 - end + h += 1 if self.exclude_end? h end end -- cgit v1.2.3 From cb870de2b52fad05c8ba0b23893008d0ba77ac3b Mon Sep 17 00:00:00 2001 From: "go.kikuta" Date: Thu, 20 Aug 2015 17:33:16 +0900 Subject: numeric.rb: refactor code (Avoid using {...} for multi-line blocks, Surrounding space missing in default value assignment) --- mrblib/numeric.rb | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/mrblib/numeric.rb b/mrblib/numeric.rb index cf608b04b..206185e78 100644 --- a/mrblib/numeric.rb +++ b/mrblib/numeric.rb @@ -100,7 +100,7 @@ module Integral # Calls the given block from +self+ to +num+ # incremented by +step+ (default 1). # - def step(num, step=1, &block) + def step(num, step = 1, &block) raise ArgumentError, "step can't be 0" if step == 0 return to_enum(:step, num, step) unless block_given? @@ -165,16 +165,12 @@ class Float # floats should be compatible to integers. def >> other n = self.to_i - other.to_i.times { - n /= 2 - } + other.to_i.times { n /= 2 } n end def << other n = self.to_i - other.to_i.times { - n *= 2 - } + other.to_i.times { n *= 2 } n.to_i end end -- cgit v1.2.3 From 11524f6f678fde684941937413b6bc2cba23b630 Mon Sep 17 00:00:00 2001 From: "go.kikuta" Date: Thu, 20 Aug 2015 17:52:30 +0900 Subject: string.rb: refactor code (remove redundant code) --- mrblib/string.rb | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/mrblib/string.rb b/mrblib/string.rb index 23cf02e97..05b13cb43 100644 --- a/mrblib/string.rb +++ b/mrblib/string.rb @@ -80,12 +80,8 @@ class String # ISO 15.2.10.5.19 def gsub!(*args, &block) str = self.gsub(*args, &block) - if str != self - self.replace(str) - self - else - nil - end + return nil if str == self + self.replace(str) end ## @@ -129,12 +125,8 @@ class String # ISO 15.2.10.5.37 def sub!(*args, &block) str = self.sub(*args, &block) - if str != self - self.replace(str) - self - else - nil - end + return nil if str == self + self.replace(str) end ## @@ -165,20 +157,16 @@ class String # Modify +self+ by replacing the content of +self+ # at the position +pos+ with +value+. def []=(pos, value) - if pos < 0 - pos += self.length - end + pos += self.length if pos < 0 b = self[0, pos] - a = self[pos+1..-1] + a = self[pos + 1..-1] self.replace([b, value, a].join('')) end ## # ISO 15.2.10.5.3 def =~(re) - if re.respond_to? :to_str - raise TypeError, "type mismatch: String given" - end + raise TypeError, "type mismatch: String given" if re.respond_to? :to_str re =~ self end -- cgit v1.2.3 From 931a7223e6ec20bfc5dbb894c74e7e7d638dc538 Mon Sep 17 00:00:00 2001 From: "go.kikuta" Date: Thu, 20 Aug 2015 18:53:13 +0900 Subject: array.rb: refactor (use onliner code if possible) --- mrblib/array.rb | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/mrblib/array.rb b/mrblib/array.rb index ef2b53725..65dd0d665 100644 --- a/mrblib/array.rb +++ b/mrblib/array.rb @@ -180,20 +180,14 @@ class Array self.delete_at(i) ret = key end - if ret.nil? && block - block.call - else - ret - end + return block.call if ret.nil? && block + ret end # internal method to convert multi-value to single value def __svalue - if self.size < 2 - self.first - else - self - end + return self.first if self.size < 2 + self end end -- cgit v1.2.3 From 85f0dd7da0fe41310883d9a65156c429135590f5 Mon Sep 17 00:00:00 2001 From: "go.kikuta" Date: Thu, 20 Aug 2015 18:53:56 +0900 Subject: enum.rb: refactor code (remove redundant code) --- mrblib/enum.rb | 32 ++++++-------------------------- 1 file changed, 6 insertions(+), 26 deletions(-) diff --git a/mrblib/enum.rb b/mrblib/enum.rb index e979fe90e..f0c9a4884 100644 --- a/mrblib/enum.rb +++ b/mrblib/enum.rb @@ -24,17 +24,9 @@ module Enumerable # ISO 15.3.2.2.1 def all?(&block) if block - self.each{|*val| - unless block.call(*val) - return false - end - } + self.each{|*val| return false unless block.call(*val)} else - self.each{|*val| - unless val.__svalue - return false - end - } + self.each{|*val| return false unless val.__svalue} end true end @@ -49,17 +41,9 @@ module Enumerable # ISO 15.3.2.2.2 def any?(&block) if block - self.each{|*val| - if block.call(*val) - return true - end - } + self.each{|*val| return true if block.call(*val)} else - self.each{|*val| - if val.__svalue - return true - end - } + self.each{|*val| return true if val.__svalue} end false end @@ -75,9 +59,7 @@ module Enumerable return to_enum :collect unless block ary = [] - self.each{|*val| - ary.push(block.call(*val)) - } + self.each{|*val| ary.push(block.call(*val))} ary end @@ -183,9 +165,7 @@ module Enumerable # ISO 15.3.2.2.10 def include?(obj) self.each{|*val| - if val.__svalue == obj - return true - end + return true if val.__svalue == obj } false end -- cgit v1.2.3