From 8dd27cb6f7477badd164dcb5d7831083143a6a38 Mon Sep 17 00:00:00 2001 From: CZagrobelny Date: Sun, 11 Feb 2018 18:31:45 -0500 Subject: [PATCH 1/8] add a section on hashes --- sites/en/intro-to-rails/ruby_language.step | 80 +++++++++++++--------- 1 file changed, 49 insertions(+), 31 deletions(-) diff --git a/sites/en/intro-to-rails/ruby_language.step b/sites/en/intro-to-rails/ruby_language.step index 0d2754848..7317009ba 100644 --- a/sites/en/intro-to-rails/ruby_language.step +++ b/sites/en/intro-to-rails/ruby_language.step @@ -15,7 +15,7 @@ steps do message "Yours might look different, but it should look something like this:" - console_without_message "irb(main):001:0>" + console_without_message "irb(main):001:0> " end step do @@ -56,6 +56,24 @@ fruits = fruits - ["kiwi"] end + step do + message "A **hash** is a dictionary-like collection of unique **keys** and their **values**." + + console <<-RUBY + fruit_dictionary = { strawberry: "a sweet soft red...", plum: "an oval, purple fruit...", orange: "a round juicy citrus fruit..." } + RUBY + + message "We're creating a hash which associates the name of a fruit (**key**) with the definition of the fruit (**value**)." + end + + step do + console <<-RUBY +fruit_dictionary[:strawberry] + RUBY + + message "Here we're using the **key**, `:strawberry`, to look up the associated **value**, the definition of 'strawberry' in the fruit dictionary." + end + step do message "Everything in Ruby has a **class**. Type this into IRB:" @@ -65,11 +83,15 @@ fruits = fruits - ["kiwi"] fruits.class RUBY - message "These are the three data types introduced so far: **Fixnum** (numbers), **String** (text), and **Array** (lists)." + message "These are the four data types introduced so far: **Fixnum** (numbers), **String** (text), **Array** (lists), and **Hash** (dictionaries)." end step do - message "Each class has different **methods** that can be used on **instances** of that class." + message <<-MARKDOWN + Each class has different **methods** that can be used on **instances** of + that class. If you're not quite sure what an instance is, [this + page on Ruby classes](http://docs.railsbridgenyc.org/ruby/classes) might help. + MARKDOWN console_without_message <<-RUBY fruits.length @@ -81,10 +103,21 @@ fruits.first fruits.methods RUBY - message "And **chain** methods together:" + message "And you can call multiple methods in a row:" console_without_message <<-RUBY fruits.methods.sort RUBY + + message <<-MARKDOWN + In `fruit.methods.sort` above, `fruit.methods` returns an array, and + `.sort` sorts that array. It's exactly like this, but without the `array` + variable: + MARKDOWN + + console_without_message <<-RUBY + array = fruits.methods + array.sort + RUBY end step do @@ -109,16 +142,6 @@ end message "This prints `YAY!` if the value stored in `my_variable` is greater than 1." message "Try changing the `>` in the conditional to a `<`." - - message "If you want to do something else when the statement evaluates to false, you can use an `else`:" - - console_without_message <<-RUBY -if my_variable > 1 - puts "YAY!" -else - puts "BOO!" -end - RUBY end step do @@ -136,25 +159,20 @@ pluralize("kiwi") end step do - message "Putting it all together, let's make a method that says your opinion of some fruits:" - message "**Don't try to type this all in!** Just paste it into irb and see what happens." - console_without_message <<-RUBY -def my_opinion(fruits) - fruits.each do |fruit| - if fruit == "pizza" - puts "pizza is the best!!!" - else - puts pluralize(fruit) + " are pretty good, I guess..." - end - end -end -my_opinion(["apple", "pizza", "orange"]) - RUBY + message "This is an *optional* practice question if you want to brush up on your Ruby. Feel free to skip it! Or talk to your TA if you get stuck, or discuss your solution once you have completed the checkpoint." - message "Try changing this method to say what your favorite fruit is." + message "Write some Ruby that prints out the names of the people in your group." + + message <<-MARKDOWN + Hints: + + 1. Start by opening up `irb`. + 1. Create the names as strings in an array. + 1. Store that array to a variable. + 1. Then use the `.each` method on the stored array to loop through each of the names. + 1. Use the `puts` method to print out the names. + MARKDOWN end end -important "Before you move on to the next step you must exit IRB by typing 'exit'" - next_step "getting_started" From 4d5767db7a5c07e36269be0383a0e93e8cd598ac Mon Sep 17 00:00:00 2001 From: CZagrobelny Date: Sun, 11 Feb 2018 18:54:59 -0500 Subject: [PATCH 2/8] cleanup and restore else explanation --- sites/en/intro-to-rails/ruby_language.step | 60 +++++++++++++--------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/sites/en/intro-to-rails/ruby_language.step b/sites/en/intro-to-rails/ruby_language.step index 7317009ba..b4f9bcef9 100644 --- a/sites/en/intro-to-rails/ruby_language.step +++ b/sites/en/intro-to-rails/ruby_language.step @@ -15,7 +15,7 @@ steps do message "Yours might look different, but it should look something like this:" - console_without_message "irb(main):001:0> " + console_without_message "irb(main):001:0>" end step do @@ -33,8 +33,8 @@ steps do step do message "You can also do math with variables:" console_without_message <<-RUBY -my_variable + 2 -my_variable * 3 + my_variable + 2 + my_variable * 3 RUBY end @@ -48,8 +48,8 @@ my_variable * 3 step do console <<-RUBY -fruits = fruits + ["orange"] -fruits = fruits - ["kiwi"] + fruits = fruits + ["orange"] + fruits = fruits - ["kiwi"] RUBY message "`+` and `-` are called operators. We can use them with the array of fruits just like we can use them with numbers." @@ -60,7 +60,7 @@ fruits = fruits - ["kiwi"] message "A **hash** is a dictionary-like collection of unique **keys** and their **values**." console <<-RUBY - fruit_dictionary = { strawberry: "a sweet soft red...", plum: "an oval, purple fruit...", orange: "a round juicy citrus fruit..." } + fruit_dictionary = { strawberry: "a sweet soft red...", plum: "an oval, purple fruit...", orange: "a round juicy citrus fruit..." } RUBY message "We're creating a hash which associates the name of a fruit (**key**) with the definition of the fruit (**value**)." @@ -68,7 +68,7 @@ fruits = fruits - ["kiwi"] step do console <<-RUBY -fruit_dictionary[:strawberry] + fruit_dictionary[:strawberry] RUBY message "Here we're using the **key**, `:strawberry`, to look up the associated **value**, the definition of 'strawberry' in the fruit dictionary." @@ -78,10 +78,10 @@ fruit_dictionary[:strawberry] message "Everything in Ruby has a **class**. Type this into IRB:" console_without_message <<-RUBY -7.class -"kiwi".class -fruits.class -RUBY + 7.class + "kiwi".class + fruits.class + RUBY message "These are the four data types introduced so far: **Fixnum** (numbers), **String** (text), **Array** (lists), and **Hash** (dictionaries)." end @@ -94,18 +94,18 @@ RUBY MARKDOWN console_without_message <<-RUBY -fruits.length -fruits.first + fruits.length + fruits.first RUBY message "You can see all the methods available for an object:" console_without_message <<-RUBY -fruits.methods + fruits.methods RUBY message "And you can call multiple methods in a row:" console_without_message <<-RUBY -fruits.methods.sort + fruits.methods.sort RUBY message <<-MARKDOWN @@ -123,9 +123,9 @@ fruits.methods.sort step do message "Arrays have a method called **each** which iterates through the list running code on each item." console_without_message <<-RUBY -fruits.each do |fruit| - puts fruit -end + fruits.each do |fruit| + puts fruit + end RUBY message "This takes the first item from the `fruits` array (`\"strawberry\"`), assigns it to the variable `fruit`, and runs the code between `do` and `end`. Then it does the same thing for each other item in the list. The code above should print a list of the fruits." end @@ -134,23 +134,33 @@ end message "A **conditional** runs code only when a statement evaluates to true." console_without_message <<-RUBY -if my_variable > 1 - puts "YAY!" -end + if my_variable > 1 + puts "YAY!" + end RUBY message "This prints `YAY!` if the value stored in `my_variable` is greater than 1." message "Try changing the `>` in the conditional to a `<`." + + message "If you want to do something else when the statement evaluates to false, you can use an `else`:" + + console_without_message <<-RUBY + if my_variable > 1 + puts "YAY!" + else + puts "BOO!" + end + RUBY end step do message "You can also make your own methods:" console_without_message <<-RUBY -def pluralize(word) - word + "s" -end -pluralize("kiwi") + def pluralize(word) + word + "s" + end + pluralize("kiwi") RUBY message "Methods take **parameters**, which are the variables they work on. In this case, we made a method called pluralize that takes one parameter, a word." From 076460791f80583f011ae629a790444521a940e9 Mon Sep 17 00:00:00 2001 From: CZagrobelny Date: Sun, 11 Feb 2018 19:01:26 -0500 Subject: [PATCH 3/8] add small group details for practice problem --- sites/en/intro-to-rails/ruby_language.step | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sites/en/intro-to-rails/ruby_language.step b/sites/en/intro-to-rails/ruby_language.step index b4f9bcef9..243f1d43e 100644 --- a/sites/en/intro-to-rails/ruby_language.step +++ b/sites/en/intro-to-rails/ruby_language.step @@ -169,7 +169,7 @@ steps do end step do - message "This is an *optional* practice question if you want to brush up on your Ruby. Feel free to skip it! Or talk to your TA if you get stuck, or discuss your solution once you have completed the checkpoint." + message "This is an *optional* small group practice question! Work in groups of 2-3 to solve the problem." message "Write some Ruby that prints out the names of the people in your group." From 6018f9507ff257a68d8a9f4276510c95e44d0c22 Mon Sep 17 00:00:00 2001 From: CZagrobelny Date: Sun, 11 Feb 2018 19:05:35 -0500 Subject: [PATCH 4/8] remove link to railsbridge instance explanation --- sites/en/intro-to-rails/ruby_language.step | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sites/en/intro-to-rails/ruby_language.step b/sites/en/intro-to-rails/ruby_language.step index 243f1d43e..22b00629e 100644 --- a/sites/en/intro-to-rails/ruby_language.step +++ b/sites/en/intro-to-rails/ruby_language.step @@ -89,8 +89,7 @@ steps do step do message <<-MARKDOWN Each class has different **methods** that can be used on **instances** of - that class. If you're not quite sure what an instance is, [this - page on Ruby classes](http://docs.railsbridgenyc.org/ruby/classes) might help. + that class. MARKDOWN console_without_message <<-RUBY From a9ed3591032f0efda874d4f8399ef01c315187a3 Mon Sep 17 00:00:00 2001 From: CZagrobelny Date: Sun, 11 Feb 2018 19:08:21 -0500 Subject: [PATCH 5/8] restore reminder to exit irb --- sites/en/intro-to-rails/ruby_language.step | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sites/en/intro-to-rails/ruby_language.step b/sites/en/intro-to-rails/ruby_language.step index 22b00629e..e95187628 100644 --- a/sites/en/intro-to-rails/ruby_language.step +++ b/sites/en/intro-to-rails/ruby_language.step @@ -87,10 +87,8 @@ steps do end step do - message <<-MARKDOWN - Each class has different **methods** that can be used on **instances** of - that class. - MARKDOWN + message "Each class has different **methods** that can be used on **instances** of + that class."" console_without_message <<-RUBY fruits.length @@ -184,4 +182,6 @@ steps do end end +important "Before you move on to the next step you must exit IRB by typing 'exit'" + next_step "getting_started" From 5e9fa7721266a6d8dde4050f44480ba179292f9d Mon Sep 17 00:00:00 2001 From: CZagrobelny Date: Sun, 11 Feb 2018 19:14:36 -0500 Subject: [PATCH 6/8] fix syntax error --- sites/en/intro-to-rails/ruby_language.step | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sites/en/intro-to-rails/ruby_language.step b/sites/en/intro-to-rails/ruby_language.step index e95187628..52f70f108 100644 --- a/sites/en/intro-to-rails/ruby_language.step +++ b/sites/en/intro-to-rails/ruby_language.step @@ -88,7 +88,7 @@ steps do step do message "Each class has different **methods** that can be used on **instances** of - that class."" + that class." console_without_message <<-RUBY fruits.length From af54c415fef3113c5cac9c0ab7c8606a662985b9 Mon Sep 17 00:00:00 2001 From: CZagrobelny Date: Sun, 11 Feb 2018 19:25:54 -0500 Subject: [PATCH 7/8] remove extraneous new line --- sites/en/intro-to-rails/ruby_language.step | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sites/en/intro-to-rails/ruby_language.step b/sites/en/intro-to-rails/ruby_language.step index 52f70f108..a46967c47 100644 --- a/sites/en/intro-to-rails/ruby_language.step +++ b/sites/en/intro-to-rails/ruby_language.step @@ -87,8 +87,7 @@ steps do end step do - message "Each class has different **methods** that can be used on **instances** of - that class." + message "Each class has different **methods** that can be used on **instances** of that class." console_without_message <<-RUBY fruits.length From 695359d0d349d2c6bfe6b56156f97aa001f00b56 Mon Sep 17 00:00:00 2001 From: CZagrobelny Date: Sun, 11 Feb 2018 19:31:20 -0500 Subject: [PATCH 8/8] eliminate unnecessary new line --- sites/en/intro-to-rails/ruby_language.step | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/sites/en/intro-to-rails/ruby_language.step b/sites/en/intro-to-rails/ruby_language.step index a46967c47..f93a04150 100644 --- a/sites/en/intro-to-rails/ruby_language.step +++ b/sites/en/intro-to-rails/ruby_language.step @@ -105,9 +105,7 @@ steps do RUBY message <<-MARKDOWN - In `fruit.methods.sort` above, `fruit.methods` returns an array, and - `.sort` sorts that array. It's exactly like this, but without the `array` - variable: + In `fruit.methods.sort` above, `fruit.methods` returns an array, and `.sort` sorts that array. It's exactly like this, but without the `array` variable: MARKDOWN console_without_message <<-RUBY