1

Why does this method only accept symbols and not strings as its arguments?

x = 10
x.respond_to?(next)

Pushing a string like the above would throw (ruby):1: void value expression.

qedk
  • 468
  • 6
  • 18

2 Answers2

3

It does accept strings:

x = 10
x.respond_to?('next')
# => true

Your problem is that you didn't pass string, but next keyword.

Marek Lipka
  • 50,622
  • 7
  • 87
  • 91
1

From Ruby respond_to? doc:

respond_to?(symbol, include_all=false) → true or false 
respond_to?(string, include_all=false) → true or false

It accepts both, symbol and string. And if string passed it is converted to a symbol.

As mentioned by Marek, you didn't pass string, neither symbol and have a syntax error with built-in keyword.

Vitalii Elenhaupt
  • 7,146
  • 3
  • 27
  • 43