FAQ: The Zen of Ruby - Call and Response

This community-built FAQ covers the “Call and Response” exercise from the lesson “The Zen of Ruby”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Ruby

FAQs on the exercise Call and Response

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 (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 (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

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.

direct link to source: https://stackoverflow.com/questions/15769739/determining-type-of-an-object-in-ruby#15769829

2 Likes

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 .)

Code/answer:
age = 26

Add your code below!

age.respond_to?(:next)

8 Likes

I have the same concern! :frowning:

@orlandosorio

.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.

2 Likes

It’s not a very good test - .respond_to will NOT definitevely tell you if it’s an integer. If you make age a string it will still respond true.

age = “blep

Add your code below!

puts age.respond_to?(:next)

puts age.next

====>
true
bleq

Hey there,

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:

age = "blep"
print age.is_a? Integer # false
2 Likes

Both of these will pass the lesson; why does this work

age.respond_to?(:next)
[age].respond_to?(:push)

but not this
[age].respond_to?(:next)

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.

ah I see. thank you very much.

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.