fork download
  1. # Assignment 9 - Tweeting with Ruby
  2. # Patrick Andrade
  3. # July 24, 2025
  4.  
  5. # Here is how you could initialize a set of tweets from Superman with a Hash.
  6. # The hash key is the user name (superman) appended with a sequential integer
  7. # number that we will start at 1. The combination of the user name
  8. # and the next integer number provide a unique hash key for each Tweet.
  9.  
  10. einstein = {
  11. "einstein1" => "Imagination is more important than knowledge.",
  12. "einstein2" => "Life is like riding a bicycle. To keep your balance you must keep moving.",
  13. "einstein3" => "Try not to become a man of success, but rather try to become a man of value.",
  14. "einstein4" => "The important thing is not to stop questioning.",
  15. "einstein5" => "I have no special talents. I am only passionately curious.",
  16. "einstein6" => "Reality is merely an illusion, albeit a very persistent one."
  17. }
  18.  
  19. # Let's print our Tweets ... I put the key between parentheses to make it stand out
  20.  
  21. printf "A few thoughtful tweets from Albert Einstein:\n\n"
  22. einstein.each { |key, val| puts "(#{key}) #{val}" }
  23.  
  24. # Now let us set up our hash of Tweets for the Batman ... a bit darker ...
  25. # Similar to the superman hash, let's initialize our hash of Tweets for the Batman.
  26.  
  27. yoda = {
  28. "yoda1" => "Do or do not. There is no try.",
  29. "yoda2" => "Train yourself to let go of everything you fear to lose.",
  30. "yoda3" => "Fear is the path to the dark side.",
  31. "yoda4" => "In a dark place we find ourselves, and a little more knowledge lights our way.",
  32. "yoda5" => "Truly wonderful, the mind of a child is."
  33. }
  34.  
  35. # Here is how you could add to our Hash - just make sure your hash key is unique
  36.  
  37. yoda["yoda6"] = "The greatest teacher, failure is."
  38.  
  39. # Over time, you would like a way to automatically generate a unique hash key. Here I set
  40. # up a variable called batnum where I can increment it by 1 each time and append it to the string
  41. # "batman" to form a unique key ... in this case, our hash key is now: "batman9"
  42.  
  43. yodanum = 6
  44. yodanum += 1 # yodanum is now 7
  45. nextKey = "yoda" + yodanum.to_s() # convert yodanum value to a string, nextKey is "yoda7"
  46.  
  47. # Use our unique hash key (nextKey) to create a new Tweet in our hash
  48.  
  49. yoda[nextKey] = "Pass on what you have learned. Strength, mastery, but weakness, folly, failure also."
  50.  
  51. # We can add another one as well
  52.  
  53. yodanum += 1 # yodanum is now 8
  54. nextKey = "yoda" + yodanum.to_s()
  55.  
  56. yoda[nextKey] = "Size matters not. Look at me. Judge me by my size, do you?"
  57.  
  58. # Let's print out all the Tweets from Yoda
  59.  
  60. printf "\n\nA few wise tweets from Master Yoda:\n\n"
  61. yoda.each { |key, val| puts "(#{key}) #{val}" }
Success #stdin #stdout 0.01s 8072KB
stdin
Standard input is empty
stdout
A few thoughtful tweets from Albert Einstein:

(einstein1) Imagination is more important than knowledge.
(einstein2) Life is like riding a bicycle. To keep your balance you must keep moving.
(einstein3) Try not to become a man of success, but rather try to become a man of value.
(einstein4) The important thing is not to stop questioning.
(einstein5) I have no special talents. I am only passionately curious.
(einstein6) Reality is merely an illusion, albeit a very persistent one.


A few wise tweets from Master Yoda:

(yoda1) Do or do not. There is no try.
(yoda2) Train yourself to let go of everything you fear to lose.
(yoda3) Fear is the path to the dark side.
(yoda4) In a dark place we find ourselves, and a little more knowledge lights our way.
(yoda5) Truly wonderful, the mind of a child is.
(yoda6) The greatest teacher, failure is.
(yoda7) Pass on what you have learned. Strength, mastery, but weakness, folly, failure also.
(yoda8) Size matters not. Look at me. Judge me by my size, do you?