2014-02-09 2 views
0

나는 레일을 사용하여,이 씨앗 데이터가있어 내보기 파일에서이 문자열을 제대로 분할 할 수 있습니까?

majors = 'business/marketing: 15%|social sciences: 14%|health professions: 11%|english: 10%|engineering: 9%|psychology: 8%|biology: 7%|history: 5%' 

를, 내가 @college.majors에 액세스하고 나는 |에 분할하고, 그것을 밖으로 인쇄 할 수 있습니다. 그러나, 지금은 Google Charts을 사용하여 막대 그래프로 만들려고합니다. 그것은 다음과 같은 포맷해야 :

var popular_majors = google.visualization.arrayToDataTable([ 
      ['Major', 'Percentage'], 
      ['Psychology', 20], 
      ['Business', 15], 
      ['Engineering', 12], 
      ['Biology', 11], 
      ['Economics', 8], 
      ['History', 6], 
      ['Mathematics', 5] 
     ]); 

(가) 짝수 제대로 문자열을 분할하고 내가 메이저 리그로 홀수 멤버를 인쇄 할 수있는 방법으로 그것을 반복하고하는 것이 가능 회원은 비율로?

코드 :

var popular_majors = google.visualization.arrayToDataTable(<%= @college.majors.gsub('%', '').split('|').map{ |element| element.split(': ') }.unshift(['Major', 'Percentage']).to_s.html_safe %>) 
      var popular_majors_options = { 
       title: 'Most Popular Majors', 
       legend: 'none', 
       hAxis: {title: 'Percentage'} 
      }; 
      var chart = new google.visualization.BarChart(document.getElementById('popular_majors_chart')); 
      chart.draw(popular_majors, popular_majors_options); 

      var ethnicities = google.visualization.arrayToDataTable(<%= @college.ethnicity.gsub('%', '').split('|').map{ |element| element.split(': ') }.unshift(['Ethnicity', 'Percentage']).to_s.html_safe %>) 
      var ethnicities_options = { 
       title: 'Ethnicities', 
       legend: 'none', 
       hAxis: {title: 'Percentage'} 
      }; 
      var chart = new google.visualization.BarChart(document.getElementById('ethnicities_chart')); 
      chart.draw(ethnicities, ethnicities_options); 
+3

예, 가능합니다. – Mischa

답변

1

뭔가 :

content = "business/marketing: 15%|social sciences: 14%|health professions: 11%|english: 10%|engineering: 9%|psychology: 8%|biology: 7%|history: 5%" 
content.split('|').map{ |element| result = element.split(': '); [result[0], result[1].to_i] }.unshift(['Major', 'Percentage']) 
# => [["Major", "Percentage"], ["business/marketing", 15], ["social sciences", 14], ["health professions", 11], ["english", 10], ["engineering", 9], ["psychology", 8], ["biology", 7], ["history", 5]] 

내가보기에 자바 스크립트에있는의 점을 놓쳤다. 당신은 단순히 결과 배열에 to_s를 호출하여 해당 작업을 수행 할 수 있습니다 당신은에 분할 같은 다른 트릭을 할 수

var popular_majors = google.visualization.arrayToDataTable(<%= @college.majors.split('|').map{ |element| result = element.split(': '); [result[0], result[1].to_i] }.unshift(['Major', 'Percentage']).to_s.html_safe %>) 
+0

"내용"은 실제로 비즈니스/마케팅 : 15 % | 사회 과학 : 14 % | 보건 전문가 : 11 % | 영어 : 10 % | 공학 : 9 % | 심리학 : 8 % | 생물학 : 7 % | 역사 : 5 %'. 그게 아무것도 바뀌 었습니까? –

+0

@AdamZerner, 그러면 훨씬 쉽습니다. 업데이트 된 답변보기 ['Array # map'] (http://www.ruby-doc.org/core-2.1.0/Array.html#method-i-map)과 ['Array # unshift'] (http : //www.ruby-doc.org/core-2.1.0/Array.html#method-i-unshift) 무슨 일이 일어나고 있는지 알고 싶다면. – Mischa

+0

@AdamZerner : Google Charts에서보기에서 이것을 사용하는 방법의 예를 보여주는 업데이트 된 답변. – Mischa

0

파이프에 한 번 split 후 다시 공간. 이 같은

0
require 'json' 

majors.split('|').collect {|major| major.gsub('%', '').split(':').collect(&:strip) }.unshift(['Major', 'Percentage']).to_json 

'% |' 그리고 ':'는 덩어리와 띠를 피할 수 있지만 나에게는 그것이 부서지기 쉬운 것처럼 보입니다. 서식을 약간 변경하면 문제가 발생합니다.

관련 문제