12. call and response


#1

What am I supposed to put??

age = 26


# Add your code below!

[#whatamisupposedtoputoverhere].respond_to?(:next)

#2

What are the instructions of this exercise?


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


#4

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


#5

what is the correct code.:smile:


#6

You know the code:

age = 26

Add your code below!

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

Good for you. LOL.


#7

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.


#8

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.


#9

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.


#10

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.


#11

who is right can you help me


#12

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!