12. call and response

What am I supposed to put??

age = 26


# Add your code below!

[#whatamisupposedtoputoverhere].respond_to?(:next)

What are the instructions of this exercise?

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

Based on that instruction, what should you use with .respond_to??

what is the correct code.:smile:

You know the code:

age = 26

Add your code below!

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

Good for you. LOL.

Maybe I don’t quite understand this so I will explain my thought process.

Based on the example provided

[1, 2, 3].respond_to?(:push)

is how to use the respond_to? to check if :push would work correctly on this array.

Given this example and the task “check to see if it will .respond_to? the .next method.” where the thing we are checking is “age = 26” wouldn’t the solution be

[age].respond_to?(:next)

I have tried this and it evaluates to false. I know there are other proposed solutions in this thread but I don’t quite understand WHY they are true as opposed to this which falls in line perfectly with the example code.

This is what worked for me

 age.respond_to?(:next)

The hint says to use :next as the argument for respond_to?
So I think my answer is the one that was intended even though other answers may work.

4 Likes

Try this.
(age).respond_to?(:next)

i think this intention of this part is just to show us whether “:next” can work for “age”. it seems this part confuss most of beginner including me at first.

I forgot to reply to you it seems. Probably too late now but we never know:

Your proposition:

[age].respond_to?(:next)

can’t work because it constructs an array with age inside it. What we want is not to know if an array containing age responds to next, what we want to know is if the object age itself responds to next.

Look at @scriptsurfer03439’s answer for how to do it.

who is right can you help me

age.respond_to?(:next)

works fine for me too.

The instruction says that,

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

base from the samples, the object is an array of numbers since they are inside . The respond_to? method is trying to check if the methods .push and .to_sym can be applicable to this array.

[1, 2, 3].respond_to?(:push)

and

[1, 2, 3].respond_to?(:to_sym)

note: There’s nothing to be worry about the last phrased sentence of the instruction. It only defines how .next method works.

This is how I understand the task here. :smiley:
Hope it helps!