Skip to content

Instantly share code, notes, and snippets.

@antonfefilov
Last active December 19, 2015 00:29
Show Gist options
  • Save antonfefilov/5868697 to your computer and use it in GitHub Desktop.
Save antonfefilov/5868697 to your computer and use it in GitHub Desktop.
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
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