Uber n00b help with a Hash

Ok guys, take it easy, I’m a super n00ber with Ruby. I’m taking a
class and this week we’re covering Ruby and this is the first time i’ve
tried it.

I understand programming languages pretty well and I’ve already done
what it takes to complete this assigment, however, it was just a lot of
copy and pasting, so it didnt challenge me at all.

I want to earn extra credit by taking it one step further.

We’re making this psudeo-twitter post like app with hashes.
I’m running into a problem when i print the contents of my hash.

Here’s my code so far:

jimiNum = 0
jimiNum += 1;
nextKey = “jimiHendrix” + jimiNum.to_s()

jimiHash = Hash.new()

jimiHash.store(nextKey, “Some text 1”)
jimiHash.store(nextKey, “Some text 2”)

jimiHash.each do |key, array|
puts “[#{key}] #{array}\n”
end

My Output is:
[jimiHendrix1] Some text 2

What I want it to show is:
[jimiHendrix1] Some text
[jimiHendrix2] Some text 2
etc.
etc.

What am I missing?

Here’s the original code that will actually complete this weeks
assignment in case it helps you see where I’m trying to go with this,
i’m just trying to pretty it up and make it more efficient:
#!/usr/bin/env ruby

Here is how you could initialize a set of tweets from Jim M.on

with a Hash.

The hash key is the user name (mrMojoRisin) appended with a sequential

integer

number that we will start at 1. The combination of the user name

and the next integer number provide a unique hash key for each Tweet.

mrMojoRisin = {
“mrMojoRisin1” => “You’re all plastic soldiers in a miniature dirt
war!”,
“mrMojoRisin2” => “There are things known and things unknown and in
between are the doors.”,
“mrMojoRisin3” => “Some of the worst mistakes of my life have been
haircuts.”,
“mrMojoRisin4” => “I see myself as an intelligent, sensitive human, with
the soul of a clown
which forces me to blow it at the most important
moments.”,
“mrMojoRisin5” => “Actually I don’t remember being born, it must have
happened during one of
my black outs.”,
“mrMojoRisin6” => “Whoever controls the media, controls the mind.”,
}

Let’s print our Tweets … I put the key between parentheses to make

it stand out

printf “A few tweets from your friend Jim M.on: \n\n”
mrMojoRisin.each { |key, val| puts “(#{key}) #{val}” }

Now let us set up our hash of Tweets for the Jimi Hendrix … a bit

darker …

Similar to the mrMojoRisin hash, let’s initialize our hash of Tweets

for the Jimi Hendrix.

jimiHendrix = {
jimiHendrix1 = “When the power of love overcomes the love of power the
world will know peace.”
jimiHendrix2 = “Knowledge speaks, but wisdom listens.”
jimiHendrix3 = “Even Castles made of sand, fall into the sea,
eventually.”
}

jimiNum = 3
jimiNum += 1
nextKey = “jimiHendrix” + jimiNum.to_s()

jimiHendrix [nextKey] = “When I die, I want people to play my music, go
wild and freak out and do anything
they want to do.”
jimiHendrix [nextKey] = “In order to change the world, you have to get
your head together first.”

Let’s print out all the Tweets from the Jimi Hendrix

printf “\n\nA few darker tweets from the Jimi Hendrix (a.k.a., the Dark
Knight): \n\n”
jimiHendrix.each { |key, val| puts “(#{key}) #{val}” }

You are using the same value for nextKey, so it is getting overwritten
each time. You need to generate a new value for each nextKey, which you
could do by making nextKey a function instead:

jimiNum = 0

def nextKey
jimiNum += 1;
“jimiHendrix” + jimiNum.to_s()
end

Tyler Adams wrote in post #1153826:

jimiHash = Hash.new()

jimiHash.store(nextKey, “Some text 1”)
jimiHash.store(nextKey, “Some text 2”)

No one in the ruby world has ever used the method
Hash#store(). Cong_ratulations, you are the first! Here is the way a
well grounded rubyist would write those three lines:

jimiHash = {
nextKey => “Some text 1”,
nextKey+1 => “Some text 2”,
}

And this:

jimiNum = 0
imiNum += 1;

is equivalent to:

jimiNum = 1

And a well grounded rubyist would never use a trailing semi-colon.

Thank you both for your input, however, i still cannot manage to get it
working as I envision.

@Anand - When i make the nextKey function like you describe, I still get
the same results.