Last active
December 19, 2015 00:29
-
-
Save antonfefilov/5868697 to your computer and use it in GitHub Desktop.
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
class Seq | |
def initialize(string) | |
@string = string.to_s | |
end | |
def next | |
@string = generate(@string) | |
self | |
end | |
def to_s | |
@string | |
end | |
private | |
def generate(string) | |
old_string = string | |
counter = 1 | |
new_string = "" | |
for index_of_char in 0..old_string.size-1 | |
char = old_string[index_of_char] | |
if old_string[index_of_char] == old_string[index_of_char+1] | |
counter += 1 | |
next | |
else | |
new_string << counter.to_s | |
new_string << char.to_s | |
counter = 1 | |
end | |
end | |
new_string | |
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
require File.expand_path('Seq.rb') | |
describe Seq do | |
describe "#next" do | |
it "should return self (same instance of the Seq class)" do | |
s = Seq.new(1) | |
s.next.should eq(s) | |
end | |
it "should assign @string with next calculated string" do | |
s = Seq.new(1) | |
s.next | |
s.instance_variable_get(:@string).should == "11" | |
end | |
it "when 3 times chained should calculate new string 3 times" do | |
s = Seq.new(1) | |
s.next.next.next | |
s.instance_variable_get(:@string).should == "1211" | |
end | |
end | |
describe "#to_s" do | |
it "should return last calculated string" do | |
s = Seq.new(1) | |
s.next.next.next.next | |
s.to_s.should == "111221" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment