-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#217]
- Loading branch information
Showing
2 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
require_relative '../../spec_helper' | ||
require_relative 'fixtures/classes' | ||
|
||
describe "String#upto" do | ||
it "passes successive values, starting at self and ending at other_string, to the block" do | ||
a = [] | ||
"*+".upto("*3") { |s| a << s } | ||
a.should == ["*+", "*,", "*-", "*.", "*/", "*0", "*1", "*2", "*3"] | ||
end | ||
|
||
it "calls the block once even when start equals stop" do | ||
a = [] | ||
"abc".upto("abc") { |s| a << s } | ||
a.should == ["abc"] | ||
end | ||
|
||
it "doesn't call block with self even if self is less than stop but stop length is less than self length" do | ||
a = [] | ||
"25".upto("5") { |s| a << s } | ||
a.should == [] | ||
end | ||
|
||
it "doesn't call block if stop is less than self and stop length is less than self length" do | ||
a = [] | ||
"25".upto("1") { |s| a << s } | ||
a.should == [] | ||
end | ||
|
||
it "doesn't call the block if self is greater than stop" do | ||
a = [] | ||
"5".upto("2") { |s| a << s } | ||
a.should == [] | ||
end | ||
|
||
it "stops iterating as soon as the current value's character count gets higher than stop's" do | ||
a = [] | ||
"96".upto("AA") { |s| a << s } | ||
a.should == ["96", "97", "98", "99"] | ||
end | ||
|
||
it "returns self" do | ||
"abc".upto("abd") { }.should == "abc" | ||
"5".upto("2") { |i| i }.should == "5" | ||
end | ||
|
||
it "tries to convert other to string using to_str" do | ||
other = mock('abd') | ||
def other.to_str() "abd" end | ||
|
||
a = [] | ||
"abc".upto(other) { |s| a << s } | ||
a.should == ["abc", "abd"] | ||
end | ||
|
||
it "raises a TypeError if other can't be converted to a string" do | ||
-> { "abc".upto(123) { } }.should raise_error(TypeError) | ||
-> { "abc".upto(mock('x')){ } }.should raise_error(TypeError) | ||
end | ||
|
||
|
||
it "does not work with symbols" do | ||
-> { "a".upto(:c).to_a }.should raise_error(TypeError) | ||
end | ||
|
||
it "returns non-alphabetic characters in the ASCII range for single letters" do | ||
"9".upto("A").to_a.should == ["9", ":", ";", "<", "=", ">", "?", "@", "A"] | ||
"Z".upto("a").to_a.should == ["Z", "[", "\\", "]", "^", "_", "`", "a"] | ||
"z".upto("~").to_a.should == ["z", "{", "|", "}", "~"] | ||
end | ||
|
||
it "stops before the last value if exclusive" do | ||
a = [] | ||
"a".upto("d", true) { |s| a << s} | ||
a.should == ["a", "b", "c"] | ||
end | ||
|
||
it "works with non-ASCII ranges" do | ||
a = [] | ||
'Σ'.upto('Ω') { |s| a << s } | ||
a.should == ["Σ", "Τ", "Υ", "Φ", "Χ", "Ψ", "Ω"] | ||
end | ||
|
||
describe "on sequence of numbers" do | ||
it "calls the block as Integer#upto" do | ||
"8".upto("11").to_a.should == 8.upto(11).map(&:to_s) | ||
"8".upto("11", true).to_a.should == 8.upto(10).map(&:to_s) | ||
end | ||
end | ||
|
||
describe "when no block is given" do | ||
it "returns an enumerator" do | ||
enum = "aaa".upto("baa", true) | ||
enum.should be_an_instance_of(Enumerator) | ||
enum.count.should == 26**2 | ||
end | ||
|
||
describe "returned Enumerator" do | ||
describe "size" do | ||
it "should return nil" do | ||
"a".upto("b").size.should == nil | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,64 @@ | ||
class String | ||
alias replace initialize_copy | ||
|
||
def upto(other_string, exclusive = false) | ||
return enum_for(:upto, other_string, exclusive) unless block_given? | ||
|
||
other_string = if other_string.is_a?(String) | ||
other_string | ||
elsif other_string.respond_to?(:to_str) | ||
other_string.to_str | ||
end | ||
|
||
unless other_string.is_a?(String) | ||
raise TypeError, "no implicit conversion of #{other_string.class} into String" | ||
end | ||
|
||
nums = /^\d+$/ | ||
if self =~ nums && other_string =~ nums | ||
treat_as_int_succ = true | ||
return self if other_string.to_i < self.to_i | ||
else | ||
return self if other_string < self | ||
return self if other_string.size < self.size | ||
end | ||
|
||
special_succ = ->(s) do | ||
if treat_as_int_succ | ||
# special handling since both ends can be ints | ||
s.to_i.succ.to_s | ||
else | ||
# special handling for single characters | ||
case s | ||
when '9' | ||
':' | ||
when 'Z' | ||
'[' | ||
when 'z' | ||
'{' | ||
else | ||
s.succ | ||
end | ||
end | ||
end | ||
|
||
less_than = ->(a, b) do | ||
if treat_as_int_succ | ||
a.to_i < b.to_i | ||
else | ||
a < b | ||
end | ||
end | ||
|
||
s = self | ||
while less_than.(s, other_string) | ||
yield s | ||
s = special_succ.(s) | ||
return self if s.size > other_string.size | ||
end | ||
|
||
yield s unless exclusive | ||
|
||
self | ||
end | ||
end |