summaryrefslogtreecommitdiffhomepage
path: root/mrblib
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2015-08-22 21:19:34 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2015-08-22 21:19:34 +0900
commitfd2f2f28debdbce8c08bd91305ecf8073817dc47 (patch)
tree9c5318614b0971f3be01dcc361b351434c2d5213 /mrblib
parent71f2975a5ba93197fac6a08f21d84bc87e31c6ae (diff)
parent85f0dd7da0fe41310883d9a65156c429135590f5 (diff)
downloadmruby-fd2f2f28debdbce8c08bd91305ecf8073817dc47.tar.gz
mruby-fd2f2f28debdbce8c08bd91305ecf8073817dc47.zip
Merge pull request #2922 from gkta/refactor-mrubygem-code
Refactor mrubygem code (range.rb, numeric.rb, string.rb, array.rb, enum.rb)
Diffstat (limited to 'mrblib')
-rw-r--r--mrblib/array.rb14
-rw-r--r--mrblib/enum.rb32
-rw-r--r--mrblib/numeric.rb10
-rw-r--r--mrblib/range.rb12
-rw-r--r--mrblib/string.rb26
5 files changed, 23 insertions, 71 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
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
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
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
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