【问题标题】:I can't interpolate my hash because apparently it is nil class? Could anyone shed some light on this?我无法插入我的哈希,因为它显然是 nil 类?任何人都可以对此有所了解吗?
【发布时间】:2017-03-10 20:32:20
【问题描述】:

是否有替代迭代的方法或我可以设置的条件允许我只插入一个哈希的值?我检查了输入的结果,显然当只有一个哈希时,我保存哈希的数组消失了。我还测试了结果,它们属于 Nil 类。

def print_with_index(students)

   students.each_with_index do |student, index|
      index_plus_one = index + 1  
   puts "#{index_plus_one}. #{students[:name]} (#{students[:cohort]} cohort)"

end
end

我如何解决我的问题以及为什么哈希会这样?

完整代码:

def print_header
  puts "The students of Villains Academy"
  puts "--------------"
end

def print_footer(names)
  puts "Overall, we have #{names.count} great students"
end

def input_students
  puts "Please enter the names and then the cohort of the students"
  puts "To finish, just hit return twice"
    #created an empty array
  students = []
    #getting the first name
  name = gets.chomp
  cohort = gets.chomp.to_sym
   if cohort.empty?
    cohort = :november
   end

  if cohort !~ /january|february|march|april|may|june|july|august|september|october|november|december/
  puts "Please enter a valid month"
  puts "Warning months are case sensitive. Please enter in lowercase characters."
    cohort = gets.chomp.to_sym
  end

  while !name.empty? do
     # add the student hash to the array called students
  students << {name: name, cohort: cohort}
    if students.count > 1
       puts "Now we have #{students.count} students"
    else students.count == 1
       puts "Now we have #{students.count} student"
    end
       #getting another name from the user
    name = gets.chomp
    cohort = gets.chomp.to_sym

    if cohort.empty?
     cohort = :november
    end

    if cohort !~ /january|february|march|april|may|june|july|august|september|october|november|december/
    puts "Please enter a valid month"
    puts "Warning months are case sensitive. Please enter in lowercase characters."
    cohort = gets.chomp.to_sym
  end

 end

 bycohort = students.sort_by { |v| v[:cohort] }
  filter = students.select! { |student| student[:cohort] == :november }
 puts bycohort #This allows me to see all of my hashes before they are filtered
 puts ""

 bycohort
 filter
 end

def print_with_index(students)

students.each_with_index do |students, index|
  index_plus_one = index + 1 
  puts "#{index_plus_one}. #{students[:name]} (#{students[:cohort]} cohort)"

   end
  end

### body ###
students = input_students
print_header
print_with_index(students)
print_footer(students)

【问题讨论】:

  • each_with_index 块的第一个参数应该是student(单数)以避免混淆。此外,字符串插值中#{...} 中的内容是任何Ruby 表达式,所以"#{index + 1}. #{student[:name]}..." 很好。
  • 当我尝试调用 print_with_index 方法时,我的代码仍然出现 NilClass 错误。即使我进行了 Sean 的更改并更改了我的姓名和群组变量的范围并显式打印。

标签: ruby hash iterator iteration string-interpolation


【解决方案1】:

虽然这对我有用,但我认为使用 each_with_index 枚举,您必须传入一个哈希数组,即 [{...}、{...}、{...}],而不是单个具有多个键值的哈希

def print_with_index(students)

  students.each_with_index do |students, index|
      index_plus_one = index + 1  
      puts "#{index_plus_one}. #{students[:name]} (#{students[:cohort]} cohort)"
  end
end

print_with_index([{name:"b", cohort: "c"}]) 
# 1. b (c cohort)

【讨论】:

  • 嗯,好的。但是,如果我不知道它们各自的价值是什么?用户将输入哈希值。
  • 不知道用户输入的价值是什么意思?我认为只要键匹配(即namecohort)你的插值...[:name]...[:cohort]就可以了,并且输入被存储为数组中的哈希
  • 啊,没关系,我在那里有几分钟忘记了范围。我可以在我的输入法中添加几个@s。我仍然得到 NilClass 错误。如果您想查看,我添加了我的完整代码。
  • 我认为你得到的 NilClass 是因为你的input_students 没有返回正在存储的students,所以你可以尝试在结束语句之前添加students那个功能?抱歉,我当前的机器上没有安装 ruby​​...
  • 我发现了这个错误。我有过滤器=students.select! { |学生|学生 [:cohort] == :november }。当我删除 ! 时,无论出于何种原因,学生都不再成为 NilClass,我可以毫无问题地进行插值。好奇怪。
【解决方案2】:

您应该使用student 而不是students 作为块参数。您可以检查学生对象是否为nil

def print_with_index(students)

  students.each_with_index do |student, index|
    if !student.nil?
      index_plus_one = index + 1  
      puts "#{index_plus_one}. #{student[:name]} (#{student[:cohort]}  cohort)"
    end
  end
end

【讨论】:

  • 如果对象为 nil(单个哈希)但我仍然希望它插入到我的字符串模板中。我能做到吗?并感谢您的建议,我将更改块参数。
  • 尽可能少用行话。例如,很难理解您所说的“如果对象是 nil(单个哈希)”是什么意思。如果一个对象是nil,那么就是这样。这不是哈希。
  • ...但是无论如何,如果一个变量为 nil,那么您仍然可以打印字符串 "#{nil}"
猜你喜欢
  • 1970-01-01
  • 2020-05-24
  • 2018-12-27
  • 1970-01-01
  • 1970-01-01
  • 2020-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多