将两个字符串转换为ruby哈希?

时间:2013-08-07 03:23:53

标签: ruby hash nokogiri

现在,我有以下内容:

<% @headlines.css('a').each do | headline | %>
   <%= headline.text %>
<% end %>

<% @top_stories.css('a').each do | top_story | %>
   <%= top_story.text %>
<% end %>

Nokogiri代码)

哪个输出:

"
    Heat vs. Bulls, Lakers vs. Clippers on opening night

    Free Agent Tracker

    Player movement
"

"

"
     Heat-Bulls, Lakers-Clippers on opening night 

     Closer look at sked 

     Most intriguing 

     Official release 

     Suns' Beasley arrested for suspected drugs 
" 

我想把这两个字符串变成ruby哈希。像这样:

{:headlines => ['Kobe ahead of schedule'], etc etc, :top_stories: ['Ex-Lebron pest Stevenson pinning to join Heat', etc etc] } 

如何做到这一点?

1 个答案:

答案 0 :(得分:1)

<%= hash_res %>, <%= headline %> = {}, ''
<% @headlines.css('a').each do | headline | %>
    <%= headline %> << <%= headline.text %>
<% end %>
<%= head_res[:headlines] %> = headline

# Do the same thing for the below
<% @top_stories.css('a').each do | top_story | %>
    <%= top_story.text %>
<% end %>