Skip to content

Instantly share code, notes, and snippets.

@tamouse
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tamouse/9174122 to your computer and use it in GitHub Desktop.
Save tamouse/9174122 to your computer and use it in GitHub Desktop.
Pyramid scheme
[6] pry(main)> load 'pyramid.rb'
=> true
[7] pry(main)> pyr1 = Pyramid.new(5)
=> #<Pyramid:0x007fb08e74a9a0
@pyramid=[[1], [2, 1], [3, 2, 1], [4, 3, 2, 1], [5, 4, 3, 2, 1]],
@size=5>
[8] pry(main)> print pyr1
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
=> nil
[9] pry(main)>
class Pyramid
attr_reader :size
attr_reader :pyramid
def initialize(size)
@size = size
@pyramid = _make_pyramid
end
def to_s
@pyramid.map{|a| a.join(" ")}.map{|a| a.center(@size * 2 + 2)}.join("\n") + "\n"
end
private
def _make_pyramid
[].tap do |a|
@size.times do |n|
a << (1..(n+1)).to_a.reverse
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment