2010-06-26 2 views
0

일부 값을 데이터베이스에 저장 한 후 으로 인쇄하기가 어렵습니다. 내가 에 데이터베이스에서 데이터를 수 가져왔다 있지만, 출력은 다음과 같다 :HASH를 ARRAY로 변환하십시오.

@vars={:object=>"46789620999001", :source_id=>1, :comment=>"["This is 
my first commenttoday and tommorrow", "This is my first commenttoday 
and tommorrow", "This is my first commenttoday and tommorrow", "This 
is my first commenttoday and tommorrow", "This is my first comment", 
"This is my first comment", "its nice!!!", "Many people do not take 
this serious. In this life we have a big role to play in making 
ourselves what we would be. It is only God that can help us to live 
that life which we have planned, so we can only pray to Him who is the 
all and all in our life to help 
us."]", :title=>"", :content=>"<div>Life is beautiful. In this life, 
whatever you see is what you will use to make out your way. People 
around you can help you in many things and ways but can never live 
your life for you. It is left for you to live your life, make and take 
decisions that you think will help you in living out your dream life. 
I believe everybody has a dream life he would like to live. Whatever 
decisions one take today will go a long way in determining the kind of 
life the one will live in future.<br />Take this as an advise.Bye </ 
div><div class="blogger-post-footer"><img width='1' height='1' 
src='https://blogger.googleusercontent.com/tracker/ 
6876835757625487750-2447909010390406819?l=godwinagada.blogspot.com' 
alt='' /></div>", :author=>"["Godwin", 
"ken"]", :category=>"Reality", :post_id=>"", :date=>"2010-06-04", :FileName=>"first"} 
>] 

사람이 출력 등의 데이터의 각 참조에서 도움이 될 수 있습니다하시기 바랍니다.

@output.each { |g| 
puts g.FileName 
puts g.post_id 
} 

+1

배열의 색인 및 값은 무엇입니까? 여기에 어떤 배열도 실제로 보이지 않습니다. –

답변

2

은 당신이 원하는하지 마십시오

+0

@ Godwin 이것은 맞습니다 - 해시를 배열로 변환하고 싶지 않고 해시 사용법을 배우고 싶습니다. –

+0

안녕 Dave, 음, 내 질문이 잘못되었을 수 있습니다. 그럼, 어떻게 사용합니까? – Godwin

+0

이 경우'@ output'이 @var와 비슷한 구조의 해쉬 배열 인 경우 현재 코드 대신 g [: FileName] 및'g [: post_id]'를 사용할 수 있습니다 귀하의 블록에 라인. –

0

당신은 값으로 각 키 가리키는 키 세트를 포함하는 해시를 보유하고 있습니다. 당신이 그들을 다룰 수있는 몇 가지 방법이 있습니다 :

  1. 당신은 단지 그것을보고 디버그하기를 원한다면. 예쁜 인쇄물을로드하고 ('pp'필요) 예쁜 인쇄물을 인쇄하십시오 (pp @vars). 더 나은 선택은 Awesome Print 젬입니다.
  2. 당신이 각 쌍의 값 출력, 단지 각 액션에 대한 블록을 지나가는 반복하는 경우 :
     
    @vars.each do |key, value| 
        puts "#{key} => #{value} 
    end
0

해보십시오 쪽을, 표준 라이브러리에서. 또 다른 대안이라고 awesome_print이 있습니다

require 'pp' 
pp @vars 

, 당신은 쉽게 읽을 수있는 형식으로 해시를 인쇄해야 이들의

require 'rubygems' 
require 'ap' 
ap @vars 

어느 같을 것이다 http://rubygems.org/gems/awesome_print에서 보석 DL을 할 수 있습니다.

관련 문제