【问题标题】:Iterating over two arrays simultaneously in Ruby在 Ruby 中同时迭代两个数组
【发布时间】:2016-05-16 22:12:05
【问题描述】:

我正在做一个项目,最终结果必须有一个由不同方法(batch_badge_creator 和 assign_rooms)生成的两个数组的主列表。

我的问题在于最后一部分,即打印机方法。我需要在屏幕上输出这两种方法,并分别打印出每个数组的每个数组项。

我如何遍历房间分配数组以完成每个单独的分配?

感谢您的帮助!

def badge_maker(name)
"Hello, my name is #{name}."
end

attendees = ["Edsger", "Ada", "Charles", "Alan", "Grace", "Linus", "Matz"]

def batch_badge_creator(attendees)
  attendees.each {|x| badge_maker(x)}
  return badges 
end

def assign_rooms(room)
  room = [1, 2, 3, 4, 5, 6, 7]
  attendees.each_with_index {|x, index| "Hello #{attendees}! You will be assigned to room #{room}!"}
  return room_assignments
end

def printer(attendees)
  assign_rooms.each {|x| puts batch_badge_creator(assign_rooms)}
  puts badges_and_room_assignments
end   

我需要打印机来打印这个:

Hello, my name is Edsger. 
Hello, my name is Ada. 
Hello, my name is Charles. 
Hello, my name is Alan. 
Hello, my name is Grace. 
Hello, my name is Linus. 
Hello, my name is Matz. 
Hello, Edsger! You'll be assigned to room 1! 
Hello, Ada! You'll be assigned to room 2! 
Hello, Charles! You'll be assigned to room 3! 
Hello, Alan! You'll be assigned to room 4! 
Hello, Grace! You'll be assigned to room 5! 
Hello, Linus! You'll be assigned to room 6! 
Hello, Matz! You'll be assigned to room 7! 

【问题讨论】:

  • 你能提供一个你想要的最终结果的例子吗?
  • @Jordan 谢谢,我从您的编辑中了解了<!-- language: lang-none -->,用于改进我下面答案中的 SO 格式。

标签: arrays ruby loops methods


【解决方案1】:

好的,我做了一些改进,但我认为这就是您所追求的。密切注意以@ 开头的变量。这些是实例变量,简而言之,它们允许自己被同一类中的其他方法看到。我还把所有东西都放在了一个类中,只是进行了一些一般性的整理并添加了一些新内容。希望有用。

class HotelEvent

  def badge_maker(name)
    "Hello, my name is #{name}."
  end

  def attendees
    @attendees = ["Edsger", "Ada", "Charles", "Alan", "Grace", "Linus", "Matz"]
  end

  def batch_badge_creator
    @badges = @attendees.map {|x| badge_maker(x)}
  end

  def assign_rooms
    @room_assignments = @attendees.map.with_index {|attendee, room| "Hello #{attendee}! You will be assigned to room #{room+1}!"}
  end

  def print_badges
    @badges.each {|b| puts b}
  end

  def print_room_numbers
    @room_assignments.each {|r| puts r}
  end

  def print_all
    print_badges
    print_room_numbers
  end  

end

new_event = HotelEvent.new
new_event.attendees
new_event.batch_badge_creator
new_event.assign_rooms
new_event.print_all

输出将是:

Hello, my name is Edsger.
Hello, my name is Ada.
Hello, my name is Charles.
Hello, my name is Alan.
Hello, my name is Grace.
Hello, my name is Linus.
Hello, my name is Matz.
Hello Edsger! You will be assigned to room 1!
Hello Ada! You will be assigned to room 2!
Hello Charles! You will be assigned to room 3!
Hello Alan! You will be assigned to room 4!
Hello Grace! You will be assigned to room 5!
Hello Linus! You will be assigned to room 6!
Hello Matz! You will be assigned to room 7!

只需根据您的喜好进行编辑和修改!请记住,代码仍然可以大大改进。例如,通过添加一些 setter 方法,但我将把它留给你作为练习。祝你好运。

【讨论】:

  • 非常感谢您的帮助和时间!这看起来很棒。我喜欢你修改它的方式。
  • @LadyD。乐意效劳。另一个提示,考虑在你的类中使用initialize 方法。上网看看它的作用。此外,如果您发现我的答案或任何其他答案特别有用,请考虑通过单击相关的灰色勾号使其变为绿色来接受。作为 SO 社区的一员,您也应该为所有其他问题这样做:)
猜你喜欢
  • 2015-05-26
  • 2021-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-08
相关资源
最近更新 更多