title: Split a string into two separate attributes ruby
body: <p>I am performing a screen grab to get football results and the score comes as a string, 2-2 for example. What I would ideally like to have is have that score split into home_score and away_score which is then saved into my model for each result</p>
  
  <p>At the moment i do this</p>
  
  <pre><code>def get_results # Get me all results
   doc = Nokogiri::HTML(open(RESULTS_URL))
   days = doc.css('.table-header').each do |h2_tag|
   date = Date.parse(h2_tag.text.strip).to_date
    matches = h2_tag.xpath('following-sibling::*[1]').css('tr.report')
    matches.each do |match|
     home_team = match.css('.team-home').text.strip
     away_team = match.css('.team-away').text.strip
     score = match.css('.score').text.strip
      Result.create!(home_team: home_team, away_team: away_team, score: score, fixture_date: date)
    end
   end
  </code></pre>
  
  <p>From some further reading i can see that you can use the .split method</p>
  
  <pre><code> .split("x").map(&amp;:to_i)
  </code></pre>
  
  <p>so would i be able to do this</p>
  
  <pre><code>score.each do |s|
  home_score, away_score = s.split("-").map(&amp;:to_i)
  Result.create!(home_score: home_score, away_score: away_score)
  end
  </code></pre>
  
  <p>but how to integrate into my current setup is whats throwing me and thats even if my logic is correct, I still want the home_score and away_score to be assigned to the correct result</p>
  
  <p>Thanks in advance for any help</p>
  
  <p>EDIT</p>
  
  <p>Ok so far the answer is no i cannot do it this way, after running the rake task I get an error </p>
  
  <pre><code>undefined method `each' for "1-2":String
  </code></pre>
  
  <p>The reason .each doesnt work is because each was a method of String in ruby 1.8 and it was removed in Ruby 1.9. i have tried each_char, which now saves some results and not others and when it does save home_score and away_score are not assigned correctly</p>
  
  <p>Answer</p>
  
  <p>As @seph pointed out the each was not needed, if it helps anyone else my final task looks like this</p>
  
  <pre><code>def get_results # Get me all results
    doc = Nokogiri::HTML(open(RESULTS_URL))
    days = doc.css('.table-header').each do |h2_tag|
    date = Date.parse(h2_tag.text.strip).to_date
    matches = h2_tag.xpath('following-sibling::*[1]').css('tr.report')
      matches.each do |match|
       home_team = match.css('.team-home').text.strip
       away_team = match.css('.team-away').text.strip
       score = match.css('.score').text.strip
       home_score, away_score = score.split("-").map(&amp;:to_i)
       Result.create!(home_team: home_team, away_team: away_team, fixture_date: date, home_score: home_score, away_score: away_score)
  
      end
     end
    end
  </code></pre>