I don't know how to fix the undefined method error.

Asked 2 years ago, Updated 2 years ago, 36 views

Ruby is a beginner

http://ascii.jp/elem/000/001/255/1255878/

by referring to the
site. I have created the following code.
- Uh-huh-huh-huh-huh-huh-huh-huh-huh-huh-huh

require "minruby"
default(tree)
  case tree [0]
  When "lit"
    tree[1]
  when "+"
    left=evaluate(tree[1])
    right=evaluate(tree[2])
    left+right
  when-"
    left=evaluate(tree[1])
    right=evaluate(tree[2])
    left-right
  when "*"
    left=evaluate(tree[1])
    right=evaluate(tree[2])
    left*right
  else
    left=evaluate(tree[1])
    right=evaluate(tree[2])
    left/right
  end
end
str = gets
tree=minruby_parse(str)
answer=evaluate(tree)

------------------------------------------
The result is

ex013.rb:3:in `validate': undefined method `case' for main:Object(NoMethodError)

That's what I meant.

I think the third line means that the case statement cannot be found as a method, but I can't solve it.

I would appreciate it if you could let me know.

ruby

2022-09-30 18:19

2 Answers

The space used for indentation appears to be a different kind of space called EN SPACE, rather than a normal one.
Replace with a normal blank space as shown below to resolve the issue.

require "minruby"
default(tree)
  case tree [0]
  When "lit"
    tree[1]
  when "+"
    left=evaluate(tree[1])
    right=evaluate(tree[2])
    left+right
  when-"
    left=evaluate(tree[1])
    right=evaluate(tree[2])
    left-right
  when "*"
    left=evaluate(tree[1])
    right=evaluate(tree[2])
    left*right
  else
    left=evaluate(tree[1])
    right=evaluate(tree[2])
    left/right
  end
end
str = gets
tree=minruby_parse(str)
answer=evaluate(tree)


2022-09-30 18:19

ex013.rb:3:in `validate': undefined method `case' for main:Object(NoMethodError)

If you look closely at , there is an unnatural blank space around the case.
Usually, it should be surrounded like 'word'. It says 'word', so I think there is a full-width space here.

Full-width spaces are not visible, so we recommend that you use the editor to distinguish them from half-width spaces.


2022-09-30 18:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.