Ruby Array has the %w operator for typing out Arrays more quickly.

>> normal_arr = ["trout", "broccoli", "vegetables"]
=> ["trout", "broccoli", "vegetables"]
 
# The short hand version is this.
 
>> arr = %w[trout broccoli vegetables]
=> ["trout", "broccoli", "vegetables"]
 
# But look what happens if you add the normal Array syntax of quotes and commas
 
>> arr = %w["trout" "broccoli" "vegetables"]
=> ["\"trout\"", "\"broccoli\"", "\"vegetables\""]
 
>> arr = %w["trout", "broccoli", "vegetables"]
=> ["\"trout\",", "\"broccoli\",", "\"vegetables\""]
 
# Also note, 
# it will turn your integers into strings
# and give you fishy results if 
# you try to populate something other than a string
 
>> a = %w[1 2 3 4 5 ]
=> ["1", "2", "3", "4", "5"]
 
# Observe a class pushed into an Array
 
>> object = Struct.new(:first_name, :last_name)
=> #<Class:0x1064acb10>
 
>> obj = %w[object]
=> ["object"]
 
>> a[0]
=> "object"
 
# You cannot use the shortcut for Arrays without strings
 
>> a = [object]
=> [#<Class:0x1064acb10>]
 
>> a[0]
=> #<Class:0x1064acb10>

:)

Leave a Reply

Your email address will not be published. Required fields are marked *