开发者

Unwanted extra [" "] in Hash value on Ruby 1.9.2

I was wondering if I do something wrong here.

here is the ruby 1.9.2 code that I have just upgraded from 1.8.6

def prepares_data(hash_nodes)
    @array_of_node = Array.new
    hash_nodes.each do | node |
      nodeId = node.values_at('self')[0].split('/').last
      text = node.values_at('data')[0].values_at('name')
      puts text
      @array_of_node << { :nodeId => nodeId, :text => text } 
    end
    puts @array_of_node
    return @array_of_node
  end

So basically, the code just adda bunch of Hash to Array which is all fine. When I print text inside the loop I also get plain text result

What allergies do you have?
What dont you like?
What is your specific diet?

However, when I print out @array_of_node I get this

{:nodeId=>"7", :text=>["What allergies do 开发者_如何转开发you have?"]}
{:nodeId=>"8", :text=>["What dont you like?"]}
{:nodeId=>"9", :text=>["What is your specific diet?"]}

In the text I have extra ["..."] which I don't want. How do I get rid of that, and I really don't understand this part, because this code worked fine in ruby 1.8.7. Did I do something wrong here?

Thank you so much.


The values_at method returns an array, so I think that's why it's happening. Try passing a key instead like this:

text = node.values_at('data')[0]['name']
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜