更新时间:2022-03-25 00:28:01
那绝对是代码的味道。您应该将它们称为 [people [0]]
, [people [1]]
,...。
That is definitely a code smell. You should refer to them as [people[0]]
, [people[1]]
, ... .
但是如果您坚持要这样做,并且可以等到12月25日(Ruby 2.1),那么您可以这样做:
But if you insist on doing so, and if you can wait until December 25 (Ruby 2.1), then you can do:
people.each.with_index(1) do |person, i|
binding.local_variable_set("newperson#{i}", [person])
end