Commit 27013125 authored by KOBAYASHI Shuji's avatar KOBAYASHI Shuji

Remove duplicated `String#each_char`

parent 2b0135e8
...@@ -84,7 +84,7 @@ end ...@@ -84,7 +84,7 @@ end
assert('IO#getc', '15.2.20.5.8') do assert('IO#getc', '15.2.20.5.8') do
io = IO.new(IO.sysopen($mrbtest_io_rfname)) io = IO.new(IO.sysopen($mrbtest_io_rfname))
$mrbtest_io_msg.each_char { |ch| $mrbtest_io_msg.split("").each { |ch|
assert_equal ch, io.getc assert_equal ch, io.getc
} }
assert_equal nil, io.getc assert_equal nil, io.getc
...@@ -127,7 +127,7 @@ end ...@@ -127,7 +127,7 @@ end
assert('IO#readchar', '15.2.20.5.15') do assert('IO#readchar', '15.2.20.5.15') do
# almost same as IO#getc # almost same as IO#getc
IO.open(IO.sysopen($mrbtest_io_rfname)) do |io| IO.open(IO.sysopen($mrbtest_io_rfname)) do |io|
$mrbtest_io_msg.each_char { |ch| $mrbtest_io_msg.split("").each { |ch|
assert_equal ch, io.readchar assert_equal ch, io.readchar
} }
assert_raise(EOFError) do assert_raise(EOFError) do
......
...@@ -21,7 +21,7 @@ class Interpreter ...@@ -21,7 +21,7 @@ class Interpreter
} }
def interpret(string) def interpret(string)
@ret = "" @ret = ""
string.each_char {|b| Dispatcher[b].bind(self).call } string.split("").each {|b| Dispatcher[b].bind(self).call }
end end
end end
......
...@@ -310,11 +310,15 @@ class String ...@@ -310,11 +310,15 @@ class String
end end
end end
##
# Call the given block for each character of
# +self+.
def each_char(&block) def each_char(&block)
return to_enum :each_char unless block return to_enum :each_char unless block
pos = 0
split('').each do |i| while pos < self.size
block.call(i) block.call(self[pos])
pos += 1
end end
self self
end end
......
...@@ -657,19 +657,19 @@ assert('String#chars(UTF-8)') do ...@@ -657,19 +657,19 @@ assert('String#chars(UTF-8)') do
end if UTF8STRING end if UTF8STRING
assert('String#each_char') do assert('String#each_char') do
s = "" chars = []
"hello!".each_char do |x| "hello!".each_char do |x|
s += x chars << x
end end
assert_equal "hello!", s assert_equal ["h", "e", "l", "l", "o", "!"], chars
end end
assert('String#each_char(UTF-8)') do assert('String#each_char(UTF-8)') do
s = "" chars = []
"こんにちは世界!".each_char do |x| "こんにちは世界!".each_char do |x|
s += x chars << x
end end
assert_equal "こんにちは世界!", s assert_equal ["こ", "ん", "に", "ち", "は", "世", "界", "!"], chars
end if UTF8STRING end if UTF8STRING
assert('String#codepoints') do assert('String#codepoints') do
......
...@@ -164,18 +164,6 @@ class String ...@@ -164,18 +164,6 @@ class String
self.replace(str) self.replace(str)
end end
##
# Call the given block for each character of
# +self+.
def each_char(&block)
pos = 0
while pos < self.size
block.call(self[pos])
pos += 1
end
self
end
## ##
# Call the given block for each byte of +self+. # Call the given block for each byte of +self+.
def each_byte(&block) def each_byte(&block)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment