Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 创建库存时,如何防止人们添加负数?_Ruby - Fatal编程技术网

Ruby 创建库存时,如何防止人们添加负数?

Ruby 创建库存时,如何防止人们添加负数?,ruby,Ruby,我正在创建一个非常简单的“存储库”,作为我的第一个真正的ruby脚本。我创建了一些部分,人们可以在其中创建一个项目和起始值,但我似乎无法确定如何防止人们以0或负数递增(或递减) 我要添加的代码如下: class Item attr_accessor :name, :count def initialize (name,initCount ) @name=name.downcase @count=initCount end def add(amount) @count += amount

我正在创建一个非常简单的“存储库”,作为我的第一个真正的ruby脚本。我创建了一些部分,人们可以在其中创建一个项目和起始值,但我似乎无法确定如何防止人们以0或负数递增(或递减)

我要添加的代码如下:

class Item
 attr_accessor :name, :count
 def initialize (name,initCount )
 @name=name.downcase
 @count=initCount
end

def add(amount)
@count += amount
end

def sub(amount)
@count -= amount
end

end

def prompt()
puts @items.inspect
puts " (A)dd item\n (R)emove item\n (L)ist items\n (I)ncrease item\n (D)ecrease items\n (Q)uit "
select = [(print '?: '), gets.rstrip][1]


if (select.upcase=="A") then
puts "Add Item\nItem name"
name=[(print 'Name? : '), gets.rstrip][1]
puts "Initial Count"
count= [(print 'Count? : '), gets.rstrip][1]
@items.push(Item.new(name,count.to_i)) unless @items.index(@items.find { |l| l.name == name })

end
感谢您的帮助

类项目
class Item
    attr_accessor :name, :count
    def initialize (name,initCount )
        raise if initCount<0

        @name=name.downcase
        @count=initCount
    end

    def add(amount)
        raise if amount<0

        @count += amount
    end

    def sub(amount)
        raise if amount<0 || amount>@count

        @count -= amount
    end

end
属性访问器:名称,:计数 def初始化(名称、初始化计数)
如果initCount考虑这样组织您的代码,则引发。我可能犯了一些错误,但你会明白的

PROMPT_TEXT =
" (A)dd item
 (R)emove item
 (L)ist items
 (I)ncrease items
 (D)ecrease items
 (Q)uit ?: "

ILLEGAL_PROMPT_RESPONSE_MSG =
  "You can't enter that! What were you thinking??"
NEGATIVE_NUMBER_MSG =
  "If I've told you once, I've told you a thousand times: NO NEGATIVE NUMBERS!"
NOT_NUMBER_MSG =
  "If that's a number, it must be Roman, and they aren't allowed."
TRY_AGAIN_MSG = "Try again...

旁白:声明

name=[(print 'Name? : '), gets.rstrip][1]

让我想起一个以“abomin”开头,以“agation”结尾的词

是纯Ruby还是Rails?抱歉@iceman。这只是ruby。请不要用rails标记它,这是另一回事。对不起!一位老师建议我,我们应该把两者都贴上标签。我的坏消息不会再发生了。您可以在sub中添加if语句,并添加方法来检查
amount
是否为合法值
return if amount<0
或类似值。虽然使用
raise
是一个好的开始,但最好不要引发一般异常,而是使用更具体的异常,并说明提出例外的原因。ArgumentError可能很有用,因为参数
amount
超出范围。另外,请解释您的答案为什么有用。抛出代码就像向OP扔鱼一样。相反,解释为什么这是有用的,并教育OP知道如何钓鱼。
def add_item
  puts "Add Item\nItem name"
  print 'Name? : '
  name = gets.rstrip
  puts "Initial Count"
  print 'Count? : '
  count = gets.rstrip
  unless count =~ /\d+/
    if count =~ /-\s*\d+/
      puts NEGATIVE_NUMBER_MSG
    else
      puts NOT_NUMBER_MSG
    end
    return false
  end
  @items.push...
  true
end
name=[(print 'Name? : '), gets.rstrip][1]