Ruby Programming Exercises #1: Printing out Shapes

Excerpt from my book Learn Ruby Programming by Examples

Zhimin Zhan

--

A repost of my daughter’s article with permission. I added a few notes. This article is an adapted version of the exercises in the first chapter of the book: Learn Ruby Programming By Examples.

Printing out asterisk characters (*) in shapes is often used as beginner’s programming exercises, as they are simple, easy to understand, and visually interesting.

You can install Ruby, edit in your favorite editor and run from the command line. Or, try online Ruby sites, where you can write and run code in your browser.

Zhimin’s notes: While the solutions are shown in Ruby language, the thinking process and solution design are applicable to all programming languages. For example, I had another book “Learn Swift Programming by Examples” for similar set of exercises.

Table of Contents:
· Ex 1: Print a Triangle
· Ex 2: Print a Half-Diamond
· Ex 3: Print a Diamond
· Challenges
· Solutions
Ex 1: Print a Triangle
Ex 2: Print a Half Diamond
Ex 3: Print a Diamond
· Zhimin’s Notes

Ex 1: Print a Triangle

Write a program to print out asterisks in the triangle shape below:

--

--

Zhimin Zhan

Test automation & CT coach, author, speaker and award-winning software developer. Help teams succeed with Agile/DevOps by implementing real Continuous Testing.