Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Joshua Richardson
7,644 PointsSetting up workspaces for Ruby
Does anyone know how to use the workspaces for 'Ruby'?
I've tried setting variables then calling them in the console but nothing seems to work. All I can do is 'puts' out text.
Can anyone point me in the right direction to making variables print to the screen? I think I am being a bit blonde. But because of the way I have learnt this part hasn't been explained to me really.
2 Answers
Maciej Czuchnowski
36,441 PointsGo to Workspaces, click New, enter a name for your workspace, choose Ruby from the dropdown and Ruby template, now you should see a console on the bottom of the window and an empty list of files on the left. Click File, choose New File, name it something (test.rb), open it, write something in it, save it (this step is important! you can use Ctrl + S command on your keyboard, just make sure there is no red dot next to the name of the file) and then in the console make sure the file is there (ls) and write ruby test.rb. This should run the file.
Joshua Richardson
7,644 PointsThat makes me feel much better. I am quite annoyed I haven't used them earlier. But I will definitely start from now.
There is one thing on Code Academy that has been bugging me that you might be able to help with. I will post below:
matz = { "First name" => "Yukihiro", "Last name" => "Matsumoto", "Age" => 47, "Nationality" => "Japanese", "Nickname" => "Matz" }
matz.each { |key, value|
puts matz[key]
}
The code above worked and passed the exercise. But I don't understand if we are printing out the ''value''... why would I not write:
puts matz[value]
If you could finally clear this up for me that would be great!
Maciej Czuchnowski
36,441 PointsTo understand this, you have to be familiar with indexing of elements, which is a common thing for most, if not all programming languages.
Square brackets indicate that you want to see what lies beneath the index you are sending in the square brackets. Let's say you have a string x = "John". Each letter in this string has a default index, counting from 0. So index 0 has the letter 'J', index has the letter 'o' and so on. So if you run puts x[0], it will return 'J'.
In the example you quoted, you are, in a way, creating your own indexing - instead of numbers from 0 to 4, you set the index to be the key variable and its value to be the value variable. So when you run puts matz["First name"], you will get the value that hides under the index which was set as "First name".
Understanding how .each and | | work is very crucial for writing concise code in Ruby and you will encounter this a lot. This goes through each element in the collection of elements and temporarily assigns them to variable(s) between the | | symbols. You can then use those temporary variables inside the block and do stuff to all the elements in one line.
Hope this helped you at least a bit.
Joshua Richardson
7,644 PointsThis has again helped allot! I still find it quite difficult to grasp, but at least I understand it! Thank you.
Maciej Czuchnowski
36,441 PointsDo some experiments on your own, you will get there eventually ;)
Maciej Czuchnowski
36,441 PointsMaciej Czuchnowski
36,441 PointsEverything should look something like this, I created this a minute ago to show you:
http://pbrd.co/1odLHAG
Joshua Richardson
7,644 PointsJoshua Richardson
7,644 PointsThank you so much! You just saved my Ruby career!
Maciej Czuchnowski
36,441 PointsMaciej Czuchnowski
36,441 PointsYou're welcome. Don't hesitate to ask questions on forums, even if you think they sound stupid. I always try to lurk around and help people, because I remember how overwhelming Ruby and Rails was for me when I first started using Treehouse.