0
col = ["a", "b", "c"]
col.map! { |x| x == "b" ? x + "!" : x }

=> ["a", "b!", "c"]

what does the ? and : in the block means/doing ?

tried my best to search thru ruby doc but nothing came up.

any reading I can do in regards to whatever that those expressions are?

help!

  • search _ternary operator_ or read the docs [here](http://ruby-doc.org/core-2.4.2/doc/syntax/control_expressions_rdoc.html#label-Ternary+if) – Sagar Pandya Dec 04 '17 at 02:34

1 Answers1

0

With col.map! you are passing the code block to each element in your array, col. The code block in this example is: if x == "b" is true then do x + "!" else return x.

So ? = if that is true then do this

and

: = or else

Abdullah
  • 1,859
  • 1
  • 18
  • 28
Steven Lian
  • 44
  • 1
  • 7
  • since ? = if, : = else is there one for elsif? – Herbert Pan Dec 04 '17 at 02:59
  • After `:` you "nest" the operators `a = 3; a.zero? ? 'a' : a == 1 ? 'b' : 'c'`. – Sebastian Palma Dec 04 '17 at 03:09
  • There is no elsif because elsif implies that our code will return three or more possible values. For ternary operators, the first part of the code block will always return true or false since we are asking a true or false statement; so we can only get two possible return values. – Steven Lian Dec 04 '17 at 03:17
  • Oops, i meant to say "can" in the first line: There is no elsif because elsif implies that our code _can_ return three or more possible values – Steven Lian Dec 04 '17 at 03:23
  • `(# Reverse all words of five more more letters in a string. Return the resulting; # string, e.g., reverse_five("Looks like my luck has reversed"); => "skooL like # my luck has desrever"` def reverse_five(str) str.split(" ").each {|x| x.length >= 5 ? x.reverse! : nil}.join(" ") end reverse_five("apple banana cat") => "elppa ananab cat") guess there is no way to avoid the `: nil` huh? – Herbert Pan Dec 10 '17 at 03:32
  • sorry about the format.. I am very new to this... :( – Herbert Pan Dec 10 '17 at 03:37