Godwin
Godwin

Reputation: 37

convert HASH into ARRAY

After saving some values into the database, I am finding it difficult to print them out. Though I have been able to pull the data out of the database, the output is like the following:

@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"} 
>] 

please can someone help out in referring to each of the data in this output eg.

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

etc

Upvotes: 0

Views: 1547

Answers (3)

Joshua Cheek
Joshua Cheek

Reputation: 31726

Try pp, from the standard library.

require 'pp'
pp @vars

There is another alternative called awesome_print, you can dl the gem from http://rubygems.org/gems/awesome_print that would look like this

require 'rubygems'
require 'ap'
ap @vars

Either of these should print the hash in a format that is easier to read.

Upvotes: 0

user377519
user377519

Reputation: 16

You have a hash, which contains a set of keys with each key pointing to a value. There are several ways you can deal with them:

  1. If you want to just view it to debug it. Load pretty print (require 'pp') and pretty print it (pp @vars). An even better choice is the Awesome Print gem.
  2. If you output the value of each pair, just iterate with each passing a block for your action:
    @vars.each do |key, value|
      puts "#{key} => #{value}
    end

Upvotes: 0

davetron5000
davetron5000

Reputation: 24821

Don't you want:

@vars[:FileName]
@vars[:post_id]

Upvotes: 2

Related Questions