|
40 | 40 | "40":{"lang":"EN","title":"All together now","chapter":"N","answer":"^4$","ok":"Yes, Shakespeare wrote 4 plays in 1591","error":"Answer for the year 1591 should be 4","text":"<p>Now suppose we only want to know the names and number of plays written by Shakespeare\nin a certain year.</p>\n\n<p>Ruby lets us <strong>select</strong> values from a hash. The select method uses a block to let us\ndefine what to select and returns what it found.</p>\n\n<p>We can then use the results returned by <strong>select</strong> in the <strong>each</strong> method as before by\nsimply adding it after the select block. This is another example of method chaining, which we saw\nearlier when reversing a poem.</p>\n\n<p>I have prepared the <strong>count_plays</strong> method for you. See if you can find out how many plays\nwere written by Shakespeare in the year <strong>1591</strong>. Try this:</p>\n\n<pre><code>def count_plays(year)\n s = get_shakey\n\n s["William Shakespeare"]\n .select { |k, v|\n v["finished"] == year\n }.each { |key, val|\n puts val["title"]\n }.count\nend\n\nputs count_plays(0)\n</code></pre>\n\n<p>Did you notice that I chained the count method at the end of the each method. This gives\nus a return value for the <strong>count_plays</strong> method.</p>\n\n<p>There is another possibility for chaining here. See that lonely line <strong>s = get_shakey</strong>.\nYou could add the <strong>[“William Shakespeare”].select { |k, v|</strong> at the end of get_shakey\n(no dot needed in this case).</p>\n","load_code":"prev"}, |
41 | 41 | "41":{"lang":"EN","title":"All's Well That Ends Well","chapter":"N","answer":"All's Well That Ends Well","ok":"True","error":"Use 1605 as the second parameter","text":"<p>Okay, we got some data from the internet, we selected what we wanted and printed it out.\nWhat is left to improve? We could print the results a bit prettier. Like add the year\nand align titles and years.</p>\n\n<p>This means printing several values on one line. Ruby has a neat way of doing\nthat. It is just like printing a string like: <strong>puts “Hi, my name is Jimmy”</strong>.\nBut instead of the literal value <em>Jimmy</em> we use the value of a variable.</p>\n\n<p>First replace <strong>Jimmy</strong> with <strong>#{}</strong>. If Ruby sees a hash symbol # followed by a curly brace {\nit looks for a variable between the first brace and the following closing brace }.\nSo we can use this: <strong>“Hi, my name is #{name}”</strong>.</p>\n\n<p>Let’s change our code a bit</p>\n\n<pre><code>def print_plays(year_from, year_to)\n get_shakey["William Shakespeare"]\n .select { |k, v|\n year_from <= v["finished"] &&\n year_to >= v["finished"]\n }.each { |k, v|\n puts "#{v["title"].ljust(30)} #{v["finished"]}"\n }\nend\nprint_plays(1600, 1605)\n</code></pre>\n\n<p>I have added <strong>.ljust(30)</strong> to the title. This way the title is <em>left justified</em> with a minimum\nlength of 30 characters so the years align nicely.</p>\n\n<p>See if you can change the ouput of the program so that it shows the plays like this: <strong>1600 -> As You Like It</strong></p>\n\n<p><strong>All’s Well That Ends Well?</strong> Hey we’re not done yet, but the end is in sight!</p>\n","load_code":"prev"}, |
42 | 42 | "42":{"lang":"EN","title":"If only I knew how to make a decision","chapter":"N","answer":"","ok":"","error":"","text":"<p>Decision making, in real life this can be a real problem. Not for us though.\nRuby makes it very easy to make decisions.</p>\n\n<pre><code>if 1 < 2\n puts "It is true: 1 is less than 2"\nend\n</code></pre>\n\n<p>The key word here is <strong>if</strong>. If can be placed before a method or after a method, like so:</p>\n\n<pre><code>puts "It is true: 1 is less than 2" if 1 < 2\n</code></pre>\n\n<p><strong>If</strong> is a method that requires one parameter. That parameter can be any expression that you\nwant to test. The outcome of the expression must be either <strong>true</strong> or <strong>false</strong>.\nHere are a couple of expressions with their outcomes:</p>\n\n<pre><code>5 <= 10 # => true\n'abc' == 'def' # => false\ntrue # => true\n123456 # => true\n0 # => true\nnil # => false\n'xyz'.empty? # => false\n'a' > 5 # => error:\n # comparison of String with\n # Numeric failed\n</code></pre>\n\n<p>The <em>if expression</em> can take many forms. It can compare literal values (1 < 2),\nvariables (a < 1) and the return value of a method (‘xyz’.empty?).</p>\n","load_code":""}, |
43 | | -"43":{"lang":"EN","title":"And Now For the Startling Conclusion","chapter":"N","answer":"is not equal to 100$","ok":"That's better","error":"This can't be right","text":"<p>Did you see the 2 equal signs in <strong>‘abc’ == ‘def’</strong>?</p>\n\n<p>This <strong>==</strong> means <strong>is equal to</strong>.\nThe single equal sign that we have seen before means <strong>assign a value to a variable</strong>.</p>\n\n<p>To make it more confusing: you can use the assignment after an if method like this:</p>\n\n<pre><code>a = 0\n\nif a = 100\n puts "Expression is true, but a is now: #{a}"\nelse\n puts "#{a} is not equal to 100"\nend\n</code></pre>\n\n<p>Change the = into == and see what happens.</p>\n\n<p>I can guarantee you that you will forget typing the second equal sign regularly. I also\nstill forget it sometimes.</p>\n\n<h3>else</h3>\n\n<p>In the code above I have added an else expression. This part will be executed when the\n<strong>if test-expression</strong> evaluates to false.</p>\n\n<blockquote>\n<p>There are more variations to this if-then-else theme. You can read more\n<a href=\"http://www.ruby-doc.org/core/doc/syntax/control_expressions_rdoc.html\" target=\"_blank\">here</a>.</p>\n</blockquote>\n","load_code":""}, |
| 43 | +"43":{"lang":"EN","title":"And Now For the Startling Conclusion","chapter":"N","answer":"is not equal to 100$","ok":"That's better","error":"This can't be right","text":"<p>Did you see the 2 equal signs in <strong>‘abc’ == ‘def’</strong>?</p>\n\n<p>This <strong>==</strong> means <strong>is equal to</strong>.\nThe single equal sign that we have seen before means <strong>assign a value to a variable</strong>.</p>\n\n<p>To make it more confusing: you can use the assignment after an if method like this:</p>\n\n<pre><code>a = 0\n\nif a = 100\n puts "Expression is true, but a is now: #{a}"\nelse\n puts "#{a} is not equal to 100"\nend\n</code></pre>\n\n<p>Change the = into == and see what happens.</p>\n\n<p>I can guarantee you that you will forget typing the second equal sign regularly. I also\nstill forget it sometimes.</p>\n\n<h3>else</h3>\n\n<p>In the code above I have added an else expression. This part will be executed when the\n<strong>if test-expression</strong> evaluates to false.</p>\n\n<blockquote>\n<p>There are more variations to this if-then-else theme. You can read more\n<a href=\"http://www.ruby-doc.org/core/syntax/control_expressions_rdoc.html\" target=\"_blank\">here</a>.</p>\n</blockquote>\n","load_code":""}, |
44 | 44 | "44":{"lang":"EN","title":"Me hungry","chapter":"N","answer":"^Me not hungry","ok":"Yes","error":"No way am I hungry at 10 AM!","text":"<p>Okay, this is coming along wonderfully. This is simple stuff for you, but keep in mind that you\ndidn’t know <strong>any Ruby whatsoever</strong> just fifteen minutes ago!</p>\n\n<p>Last step. Let’s tie it all together, you know? Let’s make it chime together like a very nice set\nof glistening chimes on the beach in the maginificent sunlight!</p>\n\n<p>We’ll define two methods first and then take a decision:</p>\n\n<pre><code>def hungry?(time_of_day_in_hours)\n puts "Me hungry"\n true\nend\n\ndef eat_an(what)\n puts "Me eat #{what}\\n"\nend\n\neat_an 'apple' if hungry?(14)\n\neat_an 'apple' if hungry?(10)\n</code></pre>\n\n<p>Now see if you can change the method <strong>hungry?</strong> to display <strong>“Me not hungry”</strong> and returning false\nwhen the time is less than 12.</p>\n","load_code":""}, |
45 | 45 | "45":{"lang":"EN","title":"Summary #6 Which Means You've Come So Far","chapter":"Y","answer":"\\{\\}","ok":"Ok, that's an empty hash","error":"","text":"<p>You’re a level six Ruby cleric. I mean what a great job you’ve done. Let’s review:</p>\n\n<h3>Data</h3>\n\n<p>You loaded some data off the internet, traversed a data structure and selected values.</p>\n\n<h3>Iterating</h3>\n\n<p>You iterated all elements of a hash and you chained some more methods.</p>\n\n<h3>Pretty printing</h3>\n\n<p>And if that wasn’t enough, you formatted and printed some values in a way that is easy\nto read for humans. In fact <strong>you made a real program!</strong></p>\n\n<h3>IF</h3>\n\n<p>You learned to take control of your programs with <strong>if</strong> and <strong>else</strong> statements.</p>\n\n<h2>So</h2>\n\n<p>What could possibly be next? What could you possibly have to learn now?\nHa, this is the best part. You’ve come such a long way that we’re going\nto uncover classes. For two more short lessons and you’re done.</p>\n\n<p>Earlier, we created a hash like this:</p>\n\n<pre><code>Hash.new\n</code></pre>\n","load_code":""}, |
46 | 46 | "46":{"lang":"EN","title":"Not a School Class, a Working Class","chapter":"N","answer":"","ok":"","error":"","text":"<p>You see, the empty curly braces {} is a shortcut for Hash.new. The new method is used to make objects\nof a certain class. Think “class” as in “working class” — a specific group of objects which\nare similar, have the same jobs, the same shirts.\nWhat use is a class?</p>\n\n<h3>Blurbalizer<sup>TM</sup></h3>\n\n<p>You just had a brilliant idea for a new app. It is going to be <strong>the</strong> next instant\nmessaging platform. You want an app where people can send each other short messages. You call\nthese messages Blurbs<sup>TM</sup>. A Blurb<sup>TM</sup> has a maximum length of just 40 characters. Let’s do a mood setting too.</p>\n\n<!---The Internet has really brought back stick people and smileys out of bankruptcy. __Emote!__-->\n\n<h3>Where to start</h3>\n\n<p>Well, you might store your Blurbs<sup>TM</sup> entries in a json file, right?\nBut how would you keep track of the content of the entry and the time it was posted?\nAnd when you loaded the file, how would it look in Ruby?\nWould it be a Hash? Or an Array? Or an Array of Arrays? Or something else?</p>\n\n<h3>Class</h3>\n\n<p>I really think you’ll want to use a class. You are already familiar with many classes:\nHash, Array, String.\nLet’s make a new class (returns no output):</p>\n\n<pre><code>class Blurb\nend\n</code></pre>\n","load_code":""}, |
|
0 commit comments