D
Dave Lenhardt
Hey,
So as I am reviewing Ruby I came across the old find strings with xyz
pattern.
The code is as follows:
class WordIndex
def initialize
@index = {}
end
def add_to_index(obj, *phrases)
phrases.each do |phrase|
phrase.scan(/\w[\w']+/) do |word| # extract each word
word.downcase!
@index[word] = [] if @index[word].nil?
@index[word].push(obj)
end
end
end
def lookup(word)
@index[word.downcase]
end
end
So here is my understanding:
phrases contains the phases/patterns to look for. |phrase| is replaced
by each of the phrases to check for.
After that, I become a bit ocnfused. I know they are looking for the
phrase in each of the words (.\w\w']+/) but how does that work?
How is each string broken down into "words"? w
hy is that done anyway (why not just find the pattern and move on?).
Also, what is obj exactly (other than an object)? How is it being
formed so it can be pushed into the stack?
Finally, what does index[word] represent? I am guessing a hash...
So as I am reviewing Ruby I came across the old find strings with xyz
pattern.
The code is as follows:
class WordIndex
def initialize
@index = {}
end
def add_to_index(obj, *phrases)
phrases.each do |phrase|
phrase.scan(/\w[\w']+/) do |word| # extract each word
word.downcase!
@index[word] = [] if @index[word].nil?
@index[word].push(obj)
end
end
end
def lookup(word)
@index[word.downcase]
end
end
So here is my understanding:
phrases contains the phases/patterns to look for. |phrase| is replaced
by each of the phrases to check for.
After that, I become a bit ocnfused. I know they are looking for the
phrase in each of the words (.\w\w']+/) but how does that work?
How is each string broken down into "words"? w
hy is that done anyway (why not just find the pattern and move on?).
Also, what is obj exactly (other than an object)? How is it being
formed so it can be pushed into the stack?
Finally, what does index[word] represent? I am guessing a hash...