Find by Category

Print Pyramid Patterns with Ruby Language

Here is a quick approach to print pyramid patterns with Ruby Language. It’s a simple and easy approach that anyone can easily understand and try on their own. I am taking rows value in every for-in loop, but I have just mentioned once on the very first code block. I just updated the all the code blocks with gets method to take rows value from the user.

#1

print "Enter Number for Rows", ": "

rows = gets.chomp.to_i

for row in 0..rows
	row.times {print "*"}
	puts
end
*
**
***
****
*****

#2



print "Enter Number for Rows", ": "

rows = gets.chomp.to_i


for row in 0..rows
	(rows-row).times {print "*"}
	puts
end
*****
****
***
**
*

#3



print "Enter Number for Rows", ": "

rows = gets.chomp.to_i


for row in 0..rows
	(rows-row).times {print " "}
	row.times {print "*"}
	puts
end
    *
   **
  ***
 ****
*****

#4



print "Enter Number for Rows", ": "

rows = gets.chomp.to_i


for row in 0..rows
	row.times {print " "}
      	(rows-row).times {print "*"}
	puts
end
*****
 ****
  ***
   **
    *

#5



print "Enter Number for Rows", ": "

rows = gets.chomp.to_i


for row in 0..rows
	(rows-row).times {print " "}
	row.times {print "*"}
	(row-1).times {print "*" }
	puts
end
    *
   ***
  *****
 *******
*********

#6



print "Enter Number for Rows", ": "

rows = gets.chomp.to_i


for row in 0..rows
	row.times {print " "}
	(rows-row).times {print "*" }
	(rows-row-1).times {print "*"}
	puts
end
*********
 *******
  *****
   ***
    *

#7



print "Enter Number for Rows", ": "

rows = gets.chomp.to_i


for row in 0..rows
	(rows-row).times {print " "}
	row.times {print "*"}
	(row-1).times {print "*" }
	puts
end

for row in 0..rows
	row.times {print " "}
	(rows-row).times {print "*" }
	(rows-row-1).times {print "*"}
	puts
end
    *
   ***
  *****
 *******
*********
*********
 *******
  *****
   ***
    *
Learn Ruby Metaprogramming at Udemy

Here is the code for all the patters mentioned above in one ruby file. If you would like, you can copy the code and run it on your system.

print "Enter Number for Rows", ": "

rows = gets.chomp.to_i

for row in 0..rows
	row.times {print "*"}
	puts
end


puts "-------------------"

# decreasing pattern
for row in 0..rows
	(rows-row).times {print "*"}
	puts
end


puts "-------------------"

for row in 0..rows
	(rows-row).times {print " "}
	row.times {print "*"}
	puts
end

puts "-------------------------"
for row in 0..rows
	row.times {print " "}
      	(rows-row).times {print "*"}
	puts
end

puts '------------------------'
for row in 0..rows
	(rows-row).times {print " "}
	row.times {print "*"}
	(row-1).times {print "*" }
	puts
end

puts "=========================="

for row in 0..rows
	row.times {print " "}
	(rows-row).times {print "*" }
	(rows-row-1).times {print "*"}
	puts
end

# these two FOR loops run togather
for row in 0..rows
	(rows-row).times {print " "}
	row.times {print "*"}
	(row-1).times {print "*" }
	puts
end

for row in 0..rows
	row.times {print " "}
	(rows-row).times {print "*" }
	(rows-row-1).times {print "*"}
	puts
end



nakshatra astrology course