Ruby 为什么一个明显的条件没有得到满足?

Ruby 为什么一个明显的条件没有得到满足?,ruby,Ruby,我从事有关日程安排的任务,有类和.yml文件,我想在其中检索数据,如“开始”点、“结束”点、“价格”等。在我的类中,我有方法,我想确定站点是否等于用户选择: class Train require 'yaml' def initialize(time) @time = YAML.load_file(time) end def calc(begin:, end:) ary = @time['time'] start = begin stop =

我从事有关日程安排的任务,有类和.yml文件,我想在其中检索数据,如“开始”点、“结束”点、“价格”等。在我的类中,我有方法,我想确定站点是否等于用户选择:

class Train 
  require 'yaml'

  def initialize(time)
    @time = YAML.load_file(time)
  end

  def calc(begin:, end:)
    ary = @time['time']
    start = begin
    stop = end
    res = []
    loop do
      tmp = ary.find { |h| h['begin'] == start }
      break unless tmp
      res << tmp
      start = tmp['end']
      break if start == stop
    end
  end
end
例如,如果写入实例变量

a = Train.new("my_path_to yml") 
a.calc(begin: ':washington', end: ':tokyo')
它不会执行任何操作。即使我重构循环块并编写“for”迭代器,它也会抛出“else”条件:

for i in ary
  if i['begin'] == 'washington'
    puts "good"
  else
    puts "no way"
  end
end
这是我的.yml文件

time:
  -
    begin: :washington
    end: :briston
    time: 6
    price: 3
  -
    begin: :briston
    end: :dallas
    time: 4
    price: 2
  -
    begin: :dallas
    end: :tokyo
    time: 3.5
    price: 3
  -
    begin: :tokyo
    end: :chicago
    time: 3.5
    price: 3
  -
    begin: :chicago
    end: :dellawer
    time: 3.5
    price: 3

提前谢谢

尝试此更改,检查代码中的注释:

def calc(begin_:, end_:) # <-- don't call variables begin or end, they are reserved words
  ary = @time['time']
  start = begin_
  stop = end_
  res = []
  loop do
    tmp = ary.find { |h| h['begin'] == start }
    break unless tmp
    res << tmp
    start = tmp['end']
    break if start == stop
  end
  res # <-- return something
end

注意不要把字符串和符号弄乱。

  ary.each do |i| # for i in ary <-- pythonic! :)
    if i['begin'] == :washington # <-- should be a symbol to pass, not a string
      puts "good"
    else
      puts "no way"
    end
  end

ari.each do | i |#对于ari中的i尝试此更改,检查代码中的注释:

def calc(begin_:, end_:) # <-- don't call variables begin or end, they are reserved words
  ary = @time['time']
  start = begin_
  stop = end_
  res = []
  loop do
    tmp = ary.find { |h| h['begin'] == start }
    break unless tmp
    res << tmp
    start = tmp['end']
    break if start == stop
  end
  res # <-- return something
end

注意不要把字符串和符号弄乱。

  ary.each do |i| # for i in ary <-- pythonic! :)
    if i['begin'] == :washington # <-- should be a symbol to pass, not a string
      puts "good"
    else
      puts "no way"
    end
  end

ari.each do | i |#因为ari中的i似乎把字符串(
'foo'
)和符号(
:foo
)混在了一起,所以你的比较不匹配。中国的一句老话是:“把关键字用作变量或方法名称的人会感到悲伤。”还有一件事需要注意:将你的
require
移到文件的最顶端。你应该尽可能地直面你的依赖关系,而不是在实现中隐藏它们。似乎你把字符串(
'foo'
)和符号(
:foo
)混在了一起,所以你的比较并不匹配。中国的古老谚语:“用关键字作为变量或方法名称的人会感到悲伤。”还有一件事需要注意:将
require
移动到文件的最顶端。只要有可能,你就应该对你的依赖性直言不讳,而不是在实现中隐藏它们。我想我理解这个问题,但不明白为什么会出现OP提到的错误。假设我们有
defa(begin:);b=开始;c=99;结束;把“b={b},c={c}”;结束
。然后
a(begin:1)
显示
b=99,c=99
。在
b=begin
中,
begin
是关键字,而不是键。在OP的代码中,
end
似乎未被视为
stop=end
中的关键字。可能行必须以
end
开头,才能将
end
视为关键字。如果是这样,则
begin
块已启动,并将继续执行,直到到达下一个关键字
end
。@CarySwoveland我得到
语法错误,意外的关键字\u end---stop=end
当他说错误在
break附近时,除非tmp
。我在上面的评论中的简化示例中得到了相同的错误,更改
def a(begin:,end:)
c=end
而不是
c=99
。应该用OP的代码来解释这个问题。我想我理解这个问题,但不明白为什么会出现OP提到的错误。假设我们有
defa(begin:);b=开始;c=99;结束;把“b={b},c={c}”;结束
。然后
a(begin:1)
显示
b=99,c=99
。在
b=begin
中,
begin
是关键字,而不是键。在OP的代码中,
end
似乎未被视为
stop=end
中的关键字。可能行必须以
end
开头,才能将
end
视为关键字。如果是这样,则
begin
块已启动,并将继续执行,直到到达下一个关键字
end
。@CarySwoveland我得到
语法错误,意外的关键字\u end---stop=end
他说错误在
break附近,除非tmp
。我在上面的评论中的简化示例中得到了相同的错误,更改了
def a(begin:,end:)
c=end
,而不是
c=99