Hashes in Ruby are a helpful way to organize information. As can be seen in the screenshot below, I’ve created a new hash, named it example, and given it a couple keys and values to start. To create your own hash, just pick the name you want it to be called and set that equal to Hash.new. Since Ruby is case-sensitive, it’s important to remember to capitalize Hash.new. To set keys and values, use the format: hashname[“key”] = “value”. In my example hash, I’ve created the keys “cold” and “hot” and given them values of “pluto” and “sun” respectively.
Next, you can get input from the user and add it to the hash as a key or a value.
Although we’re still trying to get Ruby on the Mac, I did get a chance to try it out on another computer. It’s very confusing to jump straight into it, although I do think that’s the best way for me to learn it. I read through the first bit of the chapter on Ruby in the “Seven Languages in Seven Days”, and I did understand a few things. Only a few, though, so my phrasing and understanding may be off. Continue reading
I have learned to do very basic commands with Codecademy and Why’s Poignant Guide to Ruby, but, more importantly, this week I learned how to write, save, and run scripts in Ruby. It was actually surprisingly difficult to figure it out, since nearly every source I combed through online just said to save a script with .rb, then type “ruby filename.rb” in terminal. With the lack of additional information, I did not know where I should write a script, what format it should be in, etc. It was only after a lot of trial and error and one very helpful video on youtube that I understood what needed to be done. This post will take you through the steps to write a ruby script and run in it the terminal. Continue reading