【发布时间】:2018-09-11 05:53:46
【问题描述】:
我已经编写了查找“瓶子问题”的逻辑
module Bottle
class Operation
def input
puts 'Enter the number of bottles:'
num = gets.chomp.to_i
bottle_operation(num)
end
def bottle_operation(num)
while (num < 10) && (num > 0)
puts "#{num} bottles"
num -= 1
puts "One bottle open. #{num} bottles yet to be opened."
end
end
end
begin
res = Operation.new
res.input
end
end
我被要求在模块外部使用 Begin 和 End 块,因为它的使用方式不正确。这样做我得到了以下错误
module Bottle
class Operation
def input
puts 'Enter the number of bottles:'
num = gets.chomp.to_i
bottle_operation(num)
end
def bottle_operation(num)
while (num < 10) && (num > 0)
puts "#{num} bottles"
num -= 1
puts "One bottle open. #{num} bottles yet to be opened."
end
end
end
end
begin
res = Operation.new
res.input
end
错误`
':未初始化的常量操作(NameError)
使用开始和结束块的正确方法是什么?如何以及在哪里使用
【问题讨论】:
-
名称必须完全限定:
res = Bottle::Operation.new. -
感谢它的工作,但它是使用开始和结束功能的正确方法吗?
标签: ruby