fork download
  1. # Here is how you could initialize a set of tweets from Dumbledore with a Hash.
  2. # The hash key is the user name (dumbledore) appended with a sequential integer
  3. # number that we will start at 1. The combination of the user name
  4. # and the next integer number provide a unique hash key for each Tweet.
  5.  
  6. dumbledore = {
  7. "dumbledore1" => "Happy birthday Fawkes! Not sure what I'd do without your firey personality in my life. ;-D",
  8. "dumbledore2" => "Please join us for, 'Hogwarts against the Dark Arts' benefit at the Quiddich Trillenium Stadium this Saturday!",
  9. "dumbledore3" => "Put a drink it my cup, fill it up, fill it up! Liquid Luck in my cup, fill it up, fill it up!",
  10. "dumbledore4" => "Happiness can be found even in the darkest of times, if one only remembers to turn on the light.",
  11. "dumbledore5" => "@HarryPotter, where is your invisibility cloak? I can't seem to find it. XD",
  12. }
  13.  
  14. # Let's print our Tweets ... I put the key between parentheses to make it stand out
  15.  
  16. printf "Dumbledore's latest tweets: \n\n"
  17. dumbledore.each { |key, val| puts "(#{key}) #{val}" }
  18.  
  19. # Now let us set up our hash of Tweets for the Voldemort.
  20. # Similar to the dumbledore hash, let's initialize our hash of Tweets for the Voldemort.
  21.  
  22. voldemort = {
  23. "voldemort1" => "Kind of freaking out, because I can't remember for the life of me where I put one of my horcrux's...",
  24. "voldemort2" => "May I kindly remind whoever else is considering tweeting about my nose that I am, indeed, master of the dark arts. Anything else you would like to comment on?",
  25. "voldemort3" => "Skkkahssss shaahsssstttt. That's parseltoungue for, 'I couldn't care less.' ",
  26. "voldemort4" => "Don't you just love the smell of a freshly stolen wand?",
  27. "voldemort5" => "Just a reminder: Only I can live forever."
  28. }
  29.  
  30. # Here is how you could add to our Hash - just make sure your hash key is unique
  31.  
  32. voldemort ["voldemort6"] = "Nagini far surpasses the intelligence and companionship of your little fire parrot, @dumbledore."
  33. voldemort ["voldemort7"] = "There is no good and evil. There is only power, and those too weak to seek it."
  34. voldemort ["voldemort8"] = "From this day forth, you put your faith in me."
  35.  
  36. # Over time, you would like a way to automatically generate a unique hash key. Here I set
  37. # up a variable called volnum where I can increment it by 1 each time and append it to the string
  38. # "voldemort" to form a unique key ... in this case, our hash key is now: "voldemort9"
  39.  
  40. volnum = 8
  41. volnum = volnum + 1 # volnum is now 9
  42. nextKey = "voldemort" + volnum.to_s() # convert volnum value to a string, and nextKey is now "voldemort9"
  43.  
  44. # Use our unique hash key (nextKey) to create a new Tweet in our hash
  45.  
  46. voldemort [nextKey] = "@harrypotter You have allowed your friends to die for you rather than face me yourself!"
  47.  
  48. # I could do this over and over again so it is automatically done each time Batman sends a new tweet.
  49. # Here is another round ... but as a challenge, could you create a function that might add a
  50. # given tweet to the hash with a next sequential unique key?
  51.  
  52. volnum +=1; # another way to increment volnum by 1, volnum is now 10
  53. nextKey = "voldemort" + volnum.to_s() # nextKey is now "voldemort10"
  54.  
  55. # Here is another tweet added to our hash
  56.  
  57. voldemort [nextKey] = " *Points at Mondays* Avada Kedavra!"
  58.  
  59. # Let's print out all the Tweets from the Voldemort
  60.  
  61. printf "\n\nA few darker tweets from Voldemort (a.k.a., the Dark Lord): \n\n"
  62. voldemort.each { |key, val| puts "(#{key}) #{val}" }
Success #stdin #stdout 0.01s 7936KB
stdin
Standard input is empty
stdout
Dumbledore's latest tweets: 

(dumbledore1) Happy birthday Fawkes! Not sure what I'd do without your firey personality in my life. ;-D
(dumbledore2) Please join us for, 'Hogwarts against the Dark Arts' benefit at the Quiddich Trillenium Stadium this Saturday!
(dumbledore3) Put a drink it my cup, fill it up, fill it up! Liquid Luck in my cup, fill it up, fill it up!
(dumbledore4) Happiness can be found even in the darkest of times, if one only remembers to turn on the light.
(dumbledore5) @HarryPotter, where is your invisibility cloak? I can't seem to find it. XD


A few darker tweets from Voldemort (a.k.a., the Dark Lord): 

(voldemort1) Kind of freaking out, because I can't remember for the life of me where I put one of my horcrux's...
(voldemort2) May I kindly remind whoever else is considering tweeting about my nose that I am, indeed, master of the dark arts. Anything else you would like to comment on?
(voldemort3) Skkkahssss shaahsssstttt. That's parseltoungue for, 'I couldn't care less.' 
(voldemort4) Don't you just love the smell of a freshly stolen wand?
(voldemort5) Just a reminder: Only I can live forever.
(voldemort6) Nagini far surpasses the intelligence and companionship of your little fire parrot, @dumbledore.
(voldemort7) There is no good and evil. There is only power, and those too weak to seek it.
(voldemort8) From this day forth, you put your faith in me.
(voldemort9) @harrypotter You have allowed your friends to die for you rather than face me yourself!
(voldemort10)  *Points at Mondays* Avada Kedavra!