Call and Response (.respond_to)


#1



I am really confused how .respond_to works. According to the hint "Remember, you'll need to pass .respond_to? a symbol." This is confusing me. Why is that I have to pass it as a symbol? Why can't I use age.respond_to(.next) instead?


age = 26

age.respond_to?(:next)

https://www.codecademy.com/en/courses/ruby-beginner-en-1o8Mb/2/3?curriculum_id=5059f8619189a5000201fbcb#


#2

.next isn't a value, :next is a value representing the text "next", similar to a string. The respond_to? method has some particular behaviour, so if you give it other information it won't know what to do with it. As described here though, http://ruby-doc.org/core-2.4.0/Object.html#method-i-respond_to-3F it'll also accept strings

> (5).respond_to?("upto")
=> true

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.