Last active
August 29, 2015 13:56
-
-
Save tamouse/9174122 to your computer and use it in GitHub Desktop.
Pyramid scheme
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
[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)> |
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 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