2

I am a ruby beginner and I'm to create my own console-twitter for which whenever a user tweets, the app stores de tweet in a hash.

Initially I was able to successfully do this by using

timeline = []

puts "Write your tweet"

newTweet = gets.chomp

date =DateTime.now.strftime("%d/%m/%Y %H%:%M")

@timeline << { :timestamp => date , :tweet => newTweet  }

However I was told that by using => i'm using old ruby standars so I want to enhance it by using the below code:

@timeline << { timestamp: date , tweet: newTweet  }

Unfortunately I get the following error message for that line of code: syntax error, unexpected ':', expecting '='

Also I'm having doubts on how should i declare the variable timeline

@timeline = []
@timeline = {}
@timeline = Hash.new

Many thanks in advance.

André Hess
  • 239
  • 3
  • 11

1 Answers1

3

The colon notation for hashes was introduced around version 1.9.x for Ruby. You might be on version 1.8.7 if the new syntax is giving an error.

A hash is enclosed in curly braces ({}). An array is enclosed in square brackets ([]).

You can create a new hash a couple of different ways. Both are good:

my_hash1 = Hash.new
my_hash2 = {}

You can create a new Array similarly a couple of different ways:

my_array1 = Array.new
my_array2 = []

In your example, you have an array of hashes. Which looks like (using the alternate, new syntax):

timeline = [ {timestamp: tweet_date, tweet: tweet_text}, {...}, ..., {...} ]

Or using the original syntax:

timeline = [ {:timestamp => tweet_date, :tweet => tweet_text}, {...}, ..., {...} ]

There's nothing wrong with the original syntax versus the alternate. It's forward compatible with later versions of Ruby. It's just a little more verbose.

So to use timeline as an array of hashes, it is first and foremost an array and would be created as such, e.g., timeline = []. And then you can load elements that are hashes into it via:

timeline << { timestamp: tweet_date, tweet: tweet_text }
lurker
  • 56,987
  • 9
  • 69
  • 103
  • There isn't any old or new syntax, there is an [alternate (JavaScript-style) syntax that may be used to define Hash keys that are certain types of Symbol in Hash literals](http://stackoverflow.com/q/10004158/479863). – mu is too short Jun 15 '13 at 02:11
  • 2
    @muistooshort Sorry, my poor choice of words. I did say that the 'old' (alternate) syntax is still valid, which implies alternate. I edited my answer accordingly. – lurker Jun 15 '13 at 02:12
  • @muistooshort "Old" does not necessarily mean it is obsolete. It has been there for long, so it is old. Nothing wrong with that expression. – sawa Jun 15 '13 at 04:12
  • @mbratch Unfortunately I'm getting the same error, along with a new one: odd number list for Hash Here is my complete code, it's in spanish but the code is self explicatory: [link] (http://bit.ly/14CV0nt) Just for the record i'm running ruby 1.9 – André Hess Jun 15 '13 at 19:07
  • @AndréHess Your code initialized @timeline as a hash but you're using it as an array of hashes. So you need `@timeline = []` to make it an array on line 10, not `@timeline = {}`. – lurker Jun 15 '13 at 19:39