Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is meaning of 'end +' in ruby's function?

Tags:

ruby

This is ruby function :

def long_reference_name
  if suite?
    "#{recursive_access} #{recursive_view} "
  else
    ""
  end + reference_name
end

I don't understand what's the meaning of:

end + reference_name

like image 473
Akash Jain Avatar asked Dec 01 '22 19:12

Akash Jain


2 Answers

It’s not end + reference_name, it’s <previous expression> + reference_name, where <previous_expression> is:

if suite?
 "#{recursive_access} #{recursive_view} "
else
""
end

Because blocks are expressions with values in Ruby.

In other words, you have either "#{recursive_access} #{recursive_view} " + reference_name or "" + reference_name, depending on the value of suite?.

like image 78
Konrad Rudolph Avatar answered Jan 09 '23 13:01

Konrad Rudolph


This is method + called on result of if-else-end statement.

As an example see below :

m = if true  
  "abc"
else
  "xyz"
end + "mm" 
# => "abcmm"
like image 40
Marek Lipka Avatar answered Jan 09 '23 11:01

Marek Lipka