session3/challenge/4_hashes.rb
# Write first_pos, a method which takes a string and , returns a hash
# whose keys are all the words in the string, and values are the
# earliest position they were seen in the string.
#
# There will be no punctuation in the strings.
#
# first_pos "The dog and the cat and the cow" # => {"The" => 0, "dog" => 1, "and" => 2, "the" => 3, "cat" => 4, "cow" => 7}
Write first_pos, a method which takes a string and , returns a hash
whose keys are all the words in the string, and values are the
earliest position they were seen in the string.

There will be no punctuation in the strings.


Write first_pos a method which takes a string and returns a hash whose keys are all the words in the string and values are the earliest position they were seen in the string . There will be no punctuation in the strings .
def first_pos(string)
  words = {}
  string.split(" ").each_with_index { |word, i| words[word] ||= i }
  words
end
def first_pos ( string ) words = { } string . split ( " " ) . each_with_index { | word , i | words [ word ] ||= i } words end
Write first_pos, a method which takes a string and , returns a hash
whose keys are all the words in the string, and values are the
earliest position they were seen in the string.
Write first_pos a method which takes a string and returns a hash whose keys are all the words in the string and values are the earliest position they were seen in the string .
string
<not_specific>