{"id":302,"date":"2011-10-01T16:00:51","date_gmt":"2011-10-01T16:00:51","guid":{"rendered":"http:\/\/41j.com\/blog\/?p=302"},"modified":"2011-10-01T16:00:51","modified_gmt":"2011-10-01T16:00:51","slug":"first-steps-in-ruby-2-variables-and-chop","status":"publish","type":"post","link":"https:\/\/41j.com\/blog\/2011\/10\/first-steps-in-ruby-2-variables-and-chop\/","title":{"rendered":"First steps in ruby 2: Variables and chop"},"content":{"rendered":"<p>This builds on the pervious article <a href=\"http:\/\/41j.com\/blog\/2011\/09\/first-steps-in-ruby-printing-a-triangle-of-stars\/\">here<\/a>.<\/p>\n<p>Last time we had a single loop that printed an increasing number of * characters. This time we&#8217;re going to add a loop (times do) that decreases the number of *s too. We&#8217;ll end up with pile of *s that looks like this:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ ruby square.rb \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>Here&#8217;s the code:<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\nthesize = 5\r\n\r\nstar_string = &quot;*&quot;\r\nthesize.times do\r\n  puts star_string\r\n  star_string = star_string + &quot;*&quot;\r\nend\r\n\r\nthesize.times do\r\n  star_string = star_string.chop\r\n  puts star_string\r\nend\r\n<\/pre>\n<p>This time we&#8217;ve added a variable, called &#8220;thesize&#8221;. In this case it&#8217;s just a number, we can change this to represent the size of the pile of characters. Once we&#8217;ve defined &#8220;thesize&#8221; at the top of the program we can use it wherever we want to. As you write larger program you&#8217;ll see why this is useful.<\/p>\n<p>The first loop (times do) is the same as part 1, except we&#8217;re using &#8220;thesize&#8221; instead of 10. Check back and make sure you understand the difference.<\/p>\n<p>The second loop does something a little different. It makes &#8220;star_string&#8221; shorter rather than longer. &#8220;star_string.chop&#8221; removes one character from the end of the string. So it chops characters off the string and prints them.<\/p>\n<h2>Homework<\/h2>\n<p>The homework is similar to the exercise in the previous part. Hopefully it&#8217;ll reaffirm the concepts of loops and variables.<\/p>\n<p>1. Can you make the pile a little bigger or smaller? For example:<\/p>\n<pre class=\"brush: bash; 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**\r\n*\r\n<\/pre>\n<p>2. Like before, can you get it to print two little piles?<\/p>\n<pre class=\"brush: bash; 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****\r\n****\r\n***\r\n**\r\n*\r\n<\/pre>\n<p>3. Extra credit: Can you imagine how you&#8217;d get the program to print piles of varying sizes? What would you need to do?<\/p>\n<pre class=\"brush: bash; 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**\r\n***\r\n****\r\n****\r\n***\r\n**\r\n*\r\n*\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","protected":false},"excerpt":{"rendered":"<p>This builds on the pervious article here. Last time we had a single loop that printed an increasing number of * characters. This time we&#8217;re going to add a loop (times do) that decreases the number of *s too. We&#8217;ll end up with pile of *s that looks like this: $ ruby square.rb * ** [&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-302","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1RRoU-4S","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/302","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=302"}],"version-history":[{"count":1,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/302\/revisions"}],"predecessor-version":[{"id":303,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/302\/revisions\/303"}],"wp:attachment":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/media?parent=302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/categories?post=302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/tags?post=302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}