开发者

What exception is raised when using + on symbols in Ruby?

开发者 https://www.devze.com 2023-03-18 03:45 出处:网络
Doing the Ruby Koans, in the file about_symbols at line 88, I\'m not sure of the answer. This is the code:

Doing the Ruby Koans, in the file about_symbols at line 88, I'm not sure of the answer.

This is the code:

def test_symbols_cannot_be_concatenated
    # Exceptions will be pondered further farther down the path
    assert开发者_开发问答_raise(what should i put?) do
      :cats + :dogs
    end


The point of the Ruby Koans is to learn by reading and trying things out.

Open up a terminal and start irb. Then try using the + operator on two symbols. Check the error you get and substitute it as appropriate in the Koans file.

Assuming that your prompt ends in $, that will look something like this:

$ irb
irb(main):001:0> :cats + :dogs

The answer you need will be clear pretty quickly in the error that irb spits out.


To go through step by step in case your completely new to Ruby you could try:

  1. Open up a terminal
  2. Type irb at your prompt to get to the interactive ruby prompt
    • This is where you can quickly try out different Ruby things
  3. Type the command in question :cats + :dogs
  4. Review the output which will look like
NoMethodError: undefined method `+' for :cats:Symbol
  from (irb):1

The name of the exception thrown which is what you are looking for is the first thing e.g. NoMethodError

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号