{"id":182,"date":"2011-09-28T13:56:18","date_gmt":"2011-09-28T13:56:18","guid":{"rendered":"http:\/\/41j.com\/blog\/?p=182"},"modified":"2011-10-01T16:01:53","modified_gmt":"2011-10-01T16:01:53","slug":"first-steps-in-ruby-printing-a-triangle-of-stars","status":"publish","type":"post","link":"https:\/\/41j.com\/blog\/2011\/09\/first-steps-in-ruby-printing-a-triangle-of-stars\/","title":{"rendered":"First steps in ruby. Printing a triangle of stars"},"content":{"rendered":"<p>I&#8217;m going to try and write some simple ruby programs. Here&#8217;s the first one. It prints a triangle of stars like this:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n*\r\n**\r\n***\r\n****\r\n*****\r\n******\r\n*******\r\n********\r\n*********\r\n**********\r\n***********\r\n<\/pre>\n<p>And here is the code:<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\nstar_string = &quot;*&quot;\r\n\r\n10.times do\r\n    puts star_string\r\n    star_string = star_string + &quot;*&quot;\r\nend\r\n<\/pre>\n<p>I hope you can understand it. The program creates a string called &#8220;star_string&#8221;. Then it loops 10 times. Each time it loops it prints (puts) out star_string, and adds another * to the string. That way star_string gets longer and longer.<\/p>\n<p>I&#8217;m also going to show you how this is done in C++. If you don&#8217;t want to learn C++ you don&#8217;t need to look at this. But C like languages are so common it&#8217;s worth at least gaining some familiarity with the syntax.<\/p>\n<p>#include <iostream><br \/>\n#include <string><\/p>\n<p>using namespace std;<\/p>\n<p>int main() {<\/p>\n<p>  string star_string = &#8220;*&#8221;;<\/p>\n<p>  for(int n=0;n<10;n++) {\n    cout << star_string << endl;\n    star_string = star_string + \"*\";\n  }\n\n}\n[\/sourcecode]\n\nCan you modify the program so it prints more than one triangle, like this?\n\n[sourcecode language=\"html\"]\n*\n**\n***\n****\n*****\n******\n*******\n********\n*********\n**********\n*\n**\n***\n****\n*****\n******\n*******\n********\n*********\n**********\n[\/sourcecode]\n\nPost your solution in the comments sections below. Everytime someone posts a solution, I'll try to post a new example and problem the next day.\n\nThe part 2 is now <a href=\"http:\/\/41j.com\/blog\/2011\/10\/first-steps-in-ruby-2-variables-and-chop\/\">HERE<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m going to try and write some simple ruby programs. Here&#8217;s the first one. It prints a triangle of stars like this: * ** *** **** ***** ****** ******* ******** ********* ********** *********** And here is the code: star_string = &quot;*&quot; 10.times do puts star_string star_string = star_string + &quot;*&quot; end I hope you can [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-182","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1RRoU-2W","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/182","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/comments?post=182"}],"version-history":[{"count":12,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/182\/revisions"}],"predecessor-version":[{"id":192,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/182\/revisions\/192"}],"wp:attachment":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/media?parent=182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/categories?post=182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/tags?post=182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}