Skip to content

Instantly share code, notes, and snippets.

@gouravtiwari
Last active December 22, 2015 02:29
Show Gist options
  • Save gouravtiwari/6403731 to your computer and use it in GitHub Desktop.
Save gouravtiwari/6403731 to your computer and use it in GitHub Desktop.
turtuled
# Turtle shapes
# Not at all necessory, but just to show that you can define attr_accessor from module as well
module AttrLoader
def attr_loader(*attrs)
attr_accessor *attrs
end
end
class Turtle
extend AttrLoader
attr_loader :turtle_count, :length, :width
def initialize(turtle_count, length, width)
@turtle_count = turtle_count
@length = length
@width = width
end
def fibered
Fiber.new do
counter = 1
loop do
if counter >= length
counter = 1
else
if counter == 1 || length == counter
puts @turtle_count.times.collect{'*'*width}.join(' ')
else
puts @turtle_count.times.collect{'*' + ' '*(width - 2) + '*'}.join(' ')
end
counter += 1
Fiber.yield
end
end
end
end
def shapes
fiber = fibered
@length.times{ fiber.resume}
end
end
Turtle.new(3, 7, 7).shapes
#Produces
#******* ******* *******
#* * * * * *
#* * * * * *
#* * * * * *
#* * * * * *
#* * * * * *
#******* ******* *******
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment