mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Merge pull request #2922 from gkta/refactor-mrubygem-code
Refactor mrubygem code (range.rb, numeric.rb, string.rb, array.rb, enum.rb)
This commit is contained in:
+4
-10
@@ -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
|
||||
|
||||
|
||||
+6
-26
@@ -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
|
||||
|
||||
+3
-7
@@ -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
|
||||
|
||||
+3
-9
@@ -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
|
||||
|
||||
+7
-19
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user