what does ? ? mean in ruby

Functions that end with ? in Ruby are functions that only return a boolean, that is, true, or false.

When you write a function that can only return true or false, you should end the function name with a question mark.

The example you gave shows a ternary statement, which is a one-line if-statement. .nil? is a boolean function that returns true if the value is nil and false if it is not. It first checks if the function is true, or false. Then performs an if/else to assign the value (if the .nil? function returns true, it gets nil as value, else it gets the File.join(root_dir, '/') as value.

It can be rewritten like so:

if root_dir.nil?
  prefix = nil
else
  prefix = File.join(root_dir, '/')
end

Leave a Comment