I have a business with 2 employees. Each employee has many messages.
我有一个有2名员工的企业。每个员工都有很多消息。
When I run the command:
当我运行命令时:
business.employees.each do |employee|
puts employee.messages.group_by &:from
end
I get the correct output:
我得到了正确的输出:
{100=>[#<Message id: 3, content: "Needs more training", from: 100, employee_id: 1>]}
{101=>[#<Message id: 2, content: "Very lazy.", from: 101, employee_id: 2>], 102=>[#<Message id: 1, content: "Fantastic.", created_at: "2014-03-03 12:01:28", updated_at: "2014-03-03 12:01:28", from: 102, employee_id: 2>]}
But I don't want to puts the output I want to add each hash to an array so that I can display them.
但我不想把我想要的输出添加到数组中,以便我可以显示它们。
So when I run the command
所以当我运行命令时
grouped_messages = []
business.employees.each do |employee|
grouped_messages << employee.short_messages.group_by &:from
end
I get the error: expecting keyword_end
我收到错误:期待keyword_end
3 个解决方案
#1
6
You are missing parentheses, this should work:
你缺少括号,这应该工作:
grouped_messages = []
business.employees.each do |employee|
grouped_messages << employee.short_messages.group_by(&:from)
end
A better alternative should be to use map
:
一个更好的选择应该是使用map:
grouped_messages = business.employees.map do |employee|
employee.short_messages.group_by &:from
end
#2
3
Don't omit the parentheses:
不要省略括号:
grouped_messages << employee.short_messages.group_by(&:from)
#3
-1
Parenthesis is missing around &:from
which is causing error. Change it it to (&:from)
&附近缺少括号:从中导致错误。将其更改为(&:from)
#1
6
You are missing parentheses, this should work:
你缺少括号,这应该工作:
grouped_messages = []
business.employees.each do |employee|
grouped_messages << employee.short_messages.group_by(&:from)
end
A better alternative should be to use map
:
一个更好的选择应该是使用map:
grouped_messages = business.employees.map do |employee|
employee.short_messages.group_by &:from
end
#2
3
Don't omit the parentheses:
不要省略括号:
grouped_messages << employee.short_messages.group_by(&:from)
#3
-1
Parenthesis is missing around &:from
which is causing error. Change it it to (&:from)
&附近缺少括号:从中导致错误。将其更改为(&:from)