To summarize Ruby 0.3 from codeschool:
"Exclamation Points. Methods may have exclamation points in their name, which just means to impact the current data, rather than making a copy. No big deal.
Square Brackets. With these, you can target and find things. You can even replace them if necessary.
Chaining methods lets you get a lot more done in a single command. Break up a poem, reverse it"
chain the .join onto it to put it all back together
By using { } you can create an empty hash, which is also called a dictionary.
"Hashes store related information by giving reusable labels to pieces of our data."
Creating Hashes works like > Sandwiches = { } So we'll now have an empty hash that we can call with "Sandwiches". If we want to start filling our hash or dictionary we can do that by using "Sandwiches = ["Ham"] = :Delicious".
If we attempt to just type > Sandwiches = ["Ham"] we'll get an output of nil.
Something else we did was use the : colon.
"When you place a colon in front of a simple word, you get a Ruby symbol. Symbols are much cheaper than strings (in terms of computer memory.) If you need to use a word over and over in your program itself, use a symbol. Rather than having thousands of copies of that word in memory, the computer will store a symbol only once, and refer to it over and over."
If you want, instead of typing out each symbol you're going to use to describe sandwiches such as :delicious, :tastes_good, or tastes_bad you can assign the symbol to a variable using > del = :delicious I'm not sure about best practices when it comes to this but, if you try to do > Sandwiches["Rueben"] = del then call Sandwiches your output will be => {"Rueben" => :delicious}. If you want to know how many items are in your hash then you can type > Sandwiches.length and it'll return the number of items. In my example if I tried it, I would get => 2. The method name #length will work for strings, lists, arrays, and hashes.
What if we can't remember if we've already entered something for "Ham". We can check with > Sandwiches.keys and our output will be => ["Ham", "Rueben"].
What we've done is create an empty hash and given it a name. In this instance we did Sandwiches by doing > Sandwiches {}. After we created our empty hash we then began to fill it with keys and values. We created keys by using the > Sandwiches["Ham'] = :Delicious. First we called which hash we wanted to use by declaring Sandwiches, after the square brackets and quotes targeted what key we were using, then the equal assigned a value to our key. The key in this case was the symbol ":Delicious". If we try to give a hash a key without a value we're given the output of nil as I stated earlier.
The next example codeschool gives you is to create another empty hash using a different command. It seems the end result may be the same (an empty hash) but how it interacts with our other example will be different.
if it succeeds then we'll have created a new empty hash called ratings.
The next command we're asked to enter is:
=> books.values.each { |rate| ratings[rate] += 1 }
The excerpt from codeschool describes this as:
(That | in the code is called the pipe character. It's probably located right above the Enter key on your keyboard.)
This code will turn all your unique values in books...into keys within the new ratings hash. Crazy, right? Then, as it looks at each rating you originally gave in books, it will increase the count value for that rating in ratings
After you've built your new hash of count values, type ratings again to see the full tally. This new hash will show you a rating followed by the number of times you've given that rating.
The output of our last line of code (if you were following my examples and used sandwich instead of the codeschool book example):
So what it did was as they explained above, took the values in the hash we created and turned those into keys under the new ratings hash. If you're wondering what values were assigned, were the total times each value was used in the original hash. So for our sandwich example if you were to call the hash ratings, the output will be;
If we were to analyze the last line more, we can see that we're calling our hash, then using the method .values.each The first method #values created a new array from the values in our hash. The #each (this is taken from the ruby-doc.org):
"Calls block once for each key in hsh, passing the key-value pair as parameters.
If no block is given, an enumerator is returned instead.
h = { "a" => 100, "b" => 200 }
h.each {|key, value| puts "#{key} is #{value}" }
We find in the next chapter of the fourth series, that in the last line of code that we used curly braces is actually a block of code.