Ruby - 对数组中对象的方法调用

时间:2009-07-13 20:20:07

标签: ruby arrays

我正在为一所学校的Ruby项目工作,遗憾的是我的文献中找不到这个问题的答案。

我有一系列露营地,每个露营地都有一位客人。我初始化这样的地段:

lots = Array.new

for i in (1..36)
  lots[i] = Lot.new(i)
end

再向下我创建一个Guest对象,对其进行初始化,现在我想将Guest添加到我的Lot。类Lot中的方法如下所示:

def AddGuest(guest)
  @guest = guest
end

当我要调用该方法时会出现问题,因为Lot位于Array

lots[lotnumber].AddGuest(guest)

此调用给出了错误:

undefined method `+@' for #<Guest:0x2c1ff14> (NoMethodError)

我使用过需要,所以这些课程彼此了解。我很难理解Ruby,我的错误是我尝试访问AddGuest类中的Array方法吗?我习惯于在C ++中做这样的事情。

以下是完整的来源(至少相关部分)。

整个Lot课程:

class Lot

  def initialize(number)
    @gauge = rand(2000) + 2000
    @number = number
    @guest = false
  end

  def Occupied()
    return @guest
  end

  def AddGuest(guest)
    @guest = guest
  end

  def RemoveGuest()
    @guest = false
  end

end

main.rb的部分

#includes
require 'guest'
require 'lot'

#initiate comparison variables
userInput = "0"
numberOfGuests = 0
foundLot = false
guests = Array.new
lots = Array.new

#initialize lot list
for i in (1..36)
  lots[i] = Lot.new(i)
end

播放器输入省略

#make sure lot is not taken
while foundLot == false do
  lotnumber = rand(35)+1
  if lots[lotnumber].Occupied() == false then
    foundLot = "true"
  end
end
foundLot = false

guest = Guest.new(firstName, lastName, adress, phone, arrival, lotnumber)
guests.insert(numberOfGuests, guest)
numberOfGuests++

lots[lotnumber].AddGuest(guest) #this is where error hits

end

end

end

3 个答案:

答案 0 :(得分:6)

该错误似乎与您使用++运算符有关,这在C ++中非常自然地受到支持,但在Ruby中不受支持。

等效于:

numberOfGuests += 1

答案 1 :(得分:1)

一些小技巧......

[1]

稍微更习惯的方式来写这个...

for i in (1..36)
  lots[i] = Lot.new(i)
end

会......

(1..36).each { |i| lots[i] << Lot.new(i) }

[2]

要从Lot中删除访客,您可能需要将其设置为nil而不是false。这是我的建议......

class Lot

  def initialize(number)
    @gauge = rand(2000) + 2000
    @number = number
    # Don't need to set @guest -- it's nil by default.
  end

  # In Ruby, methods that return a boolean often have a "?".
  # Makes it "read better" when you call the method. (See
  # usage sample.)
  def occupied?
    ! @guest.nil?
  end

  # There's a more commonplace way to do this. See below...
  def add_guest(guest)
    @guest = guest
  end

  def remove_guest()
    @guest = nil
  end

end

使用示例:

>> lot = Lot.new(2)
=> #<Lot:0x1300920 @number=2, @gauge=3444>
>> lot.occupied
=> false
>> lot.add_guest('A guest')
=> "A guest"
>> lot.occupied?
=> true
>> lot.remove_guest
=> nil
>> lot.occupied?
=> false

拿两个......

在类定义中使用attr_accessor方法是常规的。它们会自动为您的类添加getter和setter方法。如果你想遵循常见的Ruby模式,你可以这样做而不是add_guestremove_guest ......

class Lot

  attr_accessor :number, :gauge, :guest

  def initialize(number)
    @gauge = rand(2000) + 2000
    @number = number
  end

  def occupied?
    ! @guest.nil?
  end

end

...用法

irb(main):017:0> lot = Lot.new(3)
=> #<Lot:0xb7f7fca8 @gauge=3186, @number=3>

设置批次的访客(例如add_guest)...

irb(main):019:0> lot.guest = 'A guest'
=> "A guest"
irb(main):020:0> lot.occupied?
=> true

获得很多客人......

irb(main):025:0> lot.guest
=> "A guest"

删除访客...

irb(main):021:0> lot.guest = nil
=> nil
irb(main):023:0> lot.occupied?
=> false

答案 2 :(得分:0)

通常Ruby方法名称不是大写的。约定简单:ClassName,CONSTANT,method_name。

由于您有一个Lot对象数组,因此以下情况应为真:

lots.class # => Array
lots[1].class # => Lot

应该为Lot定义调用的方法。