-1

I was wondering if anyone could explain to me what the

? w : w.capitalize

block means in:

def titlieze(title) stop_words = %w(and in the of a an) title.capitalize.split.map{|w| stop_words.include?(w) ? w : w.capitalize}.join(' ') end

Any help is very much appreciated... Thank you in advance.

  • This [post](http://stackoverflow.com/questions/4252936/how-do-i-use-the-conditional-operator-in-ruby) explains this ternary opertor – Kirill Slatin Apr 02 '15 at 01:56

3 Answers3

1

It's a ternary operator right there. This:

stop_words.include?(w) ? w : w.capitalize

Means the same as:

if stop_words.include?(w)
  w
else
  w.capitalize
end
Tiago Farias
  • 3,397
  • 1
  • 27
  • 30
0

It's a Ternary Operator.

For each word w in the title, if w is included in stop_words return w else capitalize w and return it to the mapper.

johnsorrentino
  • 2,731
  • 1
  • 16
  • 21
0

Usually in english grammar if we upcase first letter of every word in a sentence then we leave these words still in small letters

Words: and, in, the, of, a, an

So in titlieze method of ruby it is just making sure to ignore these words from the given sentence. So the below particular condition is just an IF ELSE and nothing else

stop_words.include?(w) ? w : w.capitalize

This means if w (the word being processed) exists in the array of ignore words stop_words (and, in, the, of, a, an) then just let it be in small case else upcase the first letter of the word.

Azan Abrar
  • 96
  • 4