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