Ruby on rails 填充选择标记ruby/rails

Ruby on rails 填充选择标记ruby/rails,ruby-on-rails,ruby,Ruby On Rails,Ruby,我想选择下面的标签 <select> <option value=10> 10% </option> <option value=20> 20% </option> <option value=30> 30% </option> </select> 将给出类似的结构 [["0 %", 0], ["10 %", 10], ["20 %", 20], ["30 %", 30]] 我想知道这个结构是否可

我想选择下面的标签

<select>
<option value=10> 10% </option>
<option value=20> 20% </option>
<option value=30> 30% </option>
</select>
将给出类似的结构

[["0 %", 0], ["10 %", 10], ["20 %", 20], ["30 %", 30]]
我想知道这个结构是否可以转换成这样

[#<ratio id: 10, name: "10%">, #<id: 20, name:"20%">]
要填充select_标记 或

有没有更好的方法可以做到这一点

请发表评论。

您可以尝试使用

OpenStruct是一种数据结构,类似于散列,允许 任意属性及其伴随值的定义。 这是通过使用Ruby的元编程来定义方法来实现的 在课堂上


更新 我在OpenStruct的使用中发现了一个极端情况(看起来这只是Ruby 1.8.7的情况):
OpenStruct\35; id
将返回
object\u id
for
id
,而不是字段值:

ruby-1.8.7-p352 > o = OpenStruct.new :id => 10
 => #<OpenStruct id=10> 
ruby-1.8.7-p352 > o.id
(irb):4: warning: Object#id will be deprecated; use Object#object_id
 => 70021843187380 
我建议您使用第二种方法。

您可以尝试使用第二种方法

array = [["0 %", 0], ["10 %", 10], ["20 %", 20], ["30 %", 30]]
@arr = array.collect { |sub_array| Ratio.new(:id=>sub_array.last, :name=>sub_array.first) }
OpenStruct是一种数据结构,类似于散列,允许 任意属性及其伴随值的定义。 这是通过使用Ruby的元编程来定义方法来实现的 在课堂上


更新 我在OpenStruct的使用中发现了一个极端情况(看起来这只是Ruby 1.8.7的情况):
OpenStruct\35; id
将返回
object\u id
for
id
,而不是字段值:

ruby-1.8.7-p352 > o = OpenStruct.new :id => 10
 => #<OpenStruct id=10> 
ruby-1.8.7-p352 > o.id
(irb):4: warning: Object#id will be deprecated; use Object#object_id
 => 70021843187380 

我建议您使用第二种方法。

thnx Alex。。您还可以推荐一些书籍或参考资料,从ruby中的数据结构和元编程开始。。那真的很有帮助。我目前正在从事一些rails项目,经常需要使用上述结构。@mashit,在我看来,stidy基类库的最佳位置是。如果你想更深入地学习ruby,我可以推荐Matz的ruby编程语言。为了更深入地理解元编程,你可以查阅Perotta的《元编程Ruby》一书。。我还使用了如何在新结构(使用openStruct创建的结构)中查找元素@mashit,如何查找:
@arr.find{obj | obj.id==20}}}
thnx-Alex。。您还可以推荐一些书籍或参考资料,从ruby中的数据结构和元编程开始。。那真的很有帮助。我目前正在从事一些rails项目,经常需要使用上述结构。@mashit,在我看来,stidy基类库的最佳位置是。如果你想更深入地学习ruby,我可以推荐Matz的ruby编程语言。为了更深入地理解元编程,你可以查阅Perotta的《元编程Ruby》一书。。我还使用了如何在新结构(使用openStruct创建的结构)中查找元素@mashit,如何查找:
@arr.find{| obj | obj.id==20}#=>#
我不遵循这个类。i、 我没有使用Ratio类朋友我以为你在使用Ratio类。好的,没有问题:)我没有跟随这个类。i、 我没有使用Ratio类朋友我以为你在使用Ratio类。好的,没有问题:)
array = [["0 %", 0], ["10 %", 10], ["20 %", 20], ["30 %", 30]]
@arr = array.collect { |sub_array| Ratio.new(:id=>sub_array.last, :name=>sub_array.first) }
require 'ostruct'
arr = [["0 %", 0], ["10 %", 10], ["20 %", 20], ["30 %", 30]]
p arr.map { |name, id| OpenStruct.new(:id => id, :name => name) }
# => [#<OpenStruct id=0, name="0 %">, #<OpenStruct id=10, name="10 %">, #<OpenStruct id=20, name="20 %">, #<OpenStruct id=30, name="30 %">]
ruby-1.8.7-p352 > o = OpenStruct.new :id => 10
 => #<OpenStruct id=10> 
ruby-1.8.7-p352 > o.id
(irb):4: warning: Object#id will be deprecated; use Object#object_id
 => 70021843187380 
# 1. redefine OpenStruct#id method: 
OpenStruct.__send__(:define_method, :id) { @table[:id] }

# 2. dont use 'id' as a value and use any other value:
require 'ostruct'
(0..10).map { |i| OpenStruct.new(:value => i*10, :name => "#{i*10}%") } # 'value' instead of 'id'

...
options_from_collection_for_select(@arr, 'value', 'name')
array = [["0 %", 0], ["10 %", 10], ["20 %", 20], ["30 %", 30]]
@arr = array.collect { |sub_array| Ratio.new(:id=>sub_array.last, :name=>sub_array.first) }