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 trialMateusz Leśniak
4,762 PointsHash inside round brackets
Hi, could You explain me why inside add_transaction method we can use round brackets for creating Hash? Usually inside irb if i try:
a = (description: "description") it will give me syntax error.
So how it is possible, that we are pushing hash inside an array without curly brackets, or at least => arrow?
def add_transaction(description, amount)
@transactions.push(description: description, amount: amount)
end
3 Answers
Sorin Pantis
5,138 PointsMateusz Leśniak when you use the equality operator =
you're interacting with the array instance, not just with an element, so you need to tell Ruby that you want to make that variable an array by using the square brackets []
.
If you just want to add an element, you need to push to that to the array, using the push method on the array instance. Since it's a method, it accepts the arguments in round brackets. In your example, you are pushing a hash.
Ruby's documentation on array is good in understanding how to create an array, add elements to it (push to that array), remove them and so on.
Sorin Pantis
5,138 PointsSorry for not being more clear Mateusz.
I was talking about arrays because the push
method is used for adding elements to arrays.
To initialize arrays you need to use square brackets [], for hashes you have the curly ones {}. Round brackets are only used for passing arguments to methods, so that's why you se them with the push
method. That method knows you're passing a hash because it expects any kind of object and you're passing a key-value pair, which it's identified as a hash.
So to initialize the hash you need to do it like this:
hash = {description: "description"}
or
hash = {}
hash[:description] = "description"
Nick Bollard
3,432 PointsI was confused on this as well. The key part of your answer is this: "That method knows you're passing a hash because it expects any kind of object and you're passing a key-value pair, which it's identified as a hash.". I get it now. Thanks. You could make this a lot clearer without the other stuff in my opinion. Cheers.
Mateusz Leśniak
4,762 PointsSorin, thank you for your answer, but my doubts are about hash, not array (or maybe you answerd me, but i don't really understand it yet). I'm wondering how ruby knows that it is hash inside round brackets, when we are pushing it inside an array, and ruby doesn't know if it is hash, when i'm trying to assign it to variable (also using round brackets and symbol as a key (key:value))