There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? Like () to up-vote the contribution!
After some confusion and googling i found this very usefull explenation on stackoverflow, i thought its maybe valuable for others too:
Normally type checking is not done in Ruby, but instead objects are assessed based on their ability to respond to particular methods, commonly called “Duck typing”. In other words, if it responds to the methods you want, there’s no reason to be particular about the type.
Question = how would I have known that method .next would need to be written as :next when it’s being called on when the instructions didn’t mention it. What happens here. Why do I need to write it as :next rather then .next inside the parenthesis when it is a method and not a symbol per say? Below I posted the instructions and answer.
Instructions:
Rather than checking to see if our age variable is an integer, check to see if it will .respond_to? the .next method. ( .next will return the integer immediately following the integer it’s called on, meaning 4.next will return 5 .)
.respond_to?() checks if a method works on the object. However it can only take the method as a symbol, and this works out because symbols are great for referencing methods. This is explained at the top of the exercise: “Remember when we mentioned that symbols are awesome for referencing method names? Well, .respond_to? takes a symbol and returns true if an object can receive that method and false otherwise.”
The symbol passed into .respond_to() acts as a place holder for the method and determines if the object will ‘respond to’ the method by returning true or false.
Well the purpose of .respond_to? is not to check whether the object is an integer, rather to check if the object can receive a certain method. Which in this instance, it can.
Should you want to check if your object is an integer, there’s a simpler way:
In the lesson we have something like age = 26
If we call the next method on age (i.e. age.next), we will get 27.
If we do something like [age], we have created an array with one element i.e. we have created the array [26]. We can push new elements into an array, so [age].respond_to?(:push) will evaluate to true. For example, [26].push(17) will update the array to [26, 17].
However calling the next method on an array doesn’t make sense and isn’t allowed.
So, [age].respond_to?(:next) will be false.
What is [26].next supposed to do? It doesn’t make sense, so using the next method on an array is invalid.
I have a doubt: in another context, for an instance, if I’m building a hash, can I use a symbol named after a method?
In JavaScript, you can’t have a varible named after a reserved word.
Is it the same in Ruby, towards symbols?
There is no technical problem with using a symbol in a hash with the same name as a reserved keyword. However, using something else in the hash is better for clarity’s sake for yourself and anyone else who might be accessing or maintaining your code.