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 trialTania Dimache
1,051 PointsDouble quotes mark on setting variable name
Why I can't get past the challenge if I write the variable like this: too_fast = "true"
My correct code is like this and it works:
car_speed = 65 speed_limit = 60 too_fast = true if car_speed > speed_limit puts #{too_fast} end
I remembered that for text variables, you put it inside double quotes eg: favorite_dish = "pasta"
In this case, true is text so I tried putting it inside double quotes but my challenge is correct only I remove the double quotes.
Thanks.
2 Answers
Tadeáš Firich
7,866 PointsHello Tania. This is a good question. Because Ruby is a dynamically typed language it means, if you type this:
variable = "Hello"
Ruby thinks like that hey it is in the quotes marks so it must be string.
In the next example:
variable = 123
Ruby thinks this is a number because there isn't a quotes marks.
In the last example:
variable = true
another_variable = false
there is the special types of variables call Boolean.
And this is the right code of this challenge: :)
car_speed = 65
speed_limit = 60
if car_speed > speed_limit
too_fast = true
end
Does it make sense?
Ari Misha
19,323 PointsHiya there! Anything that gets wrapped around quotes get interpolated as a string. Its not just Ruby, its kinda like computer programming basics or something. But always be aware of difference between single quotes , double quotes , variables and booleans.
Tadeáš Firich nailed the explanation. But i'd like to add few stuff to the explanation. Booleans are singletons in Ruby. It means in OOPs(object oriented programming), they're the only instances of class True and False. Get into irb and you can check it yourself by typing true.class
or false.class
. To define a function which returns boolean , in Ruby of course, ends with "?". Its not a necessity, its just Ruby convention about how to write codes.
One more thing i'd like to add is the difference between single quotes and Double quotes. If you'd like to do String interpolation, always use double quotes. BUT if you'd like to escape those double quotes and wants to print in your statement use single quotes.
I know its not relevant to the challenge but its really helpful to get the good grasp of the fundamentals of Ruby. You have a nice one. (:
Tania Dimache
1,051 PointsTania Dimache
1,051 PointsHi Tadeáš!
Thanks! So, true and false are Boolean variables and for those you cannot use double quotes?
I changed so much my code that in the end it worked as I wrote it above :)), but indeed no double quotes
Tadeáš Firich
7,866 PointsTadeáš Firich
7,866 PointsTania exactly. No quotes for boolean variables. Because i think you can youse single '' or double "" quotes for defining a string variable. I have looked for some resources for this topic but i couldn't find any simple example of this topic. :)
if it is helpful for you you can give me some points. :) Simply check previous answer as corect. Have a nice code.