2013-08-20 65 views
0

我觉得我错过了一些非常明显的东西。Ruby on Rails:无法将哈希值转换为浮点数

我有以下散列放置在变量“simple_lable”。调用它检查工作正常,但我似乎无法访问任何数值。 .to_i出来为0,.to_f出来为0.0和to_s出来空白

puts "**************************" 
puts simple_label.inspect 
puts simple_label["margin_top"].to_f 
puts simple_label["margin_bottom"].to_f 
puts simple_label["margin_right"].to_f 
puts simple_label["margin_left"].to_f 
puts simple_label["paper_size"] 
puts "**************************" 

结果

************************** 
{"paper_size"=>"LETTER", "top_margin"=>36, "bottom_margin"=>36, "left_margin"=>15.822, "right_margin"=>15.822, "columns"=>3, "rows"=>10, "column_gutter"=>15, "row_gutter"=>0} 
0.0 
0.0 
0.0 
0.0 
LETTER 
************************** 

所以价值是有的,但我似乎无法得到保持正确的。

任何想法?

非常感谢。

回答

0

问题是你的散列键是不正确的。例如,你的散列值有top_margin作为关键字,但你试图引用margin_top的关键字。对于不存在的密钥,散列将返回nil,并且to_f,to_i给出零,并且对于nil散列值给出空白to_s

+0

你是如此的正确(拍拍额头!)。我想有时它只是需要另一双眼睛,非常感谢:) –

+0

@BjornForsberg没有问题。我一眼就看不到它。 – lurker