What does "end +" mean in ruby ​​function?

This is the ruby ​​function:

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

I do not understand what it means:

end + reference_name

+4
source share
3 answers

Its not end + reference_name , its <previous expression> + reference_name , where <previous_expression> :

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

Since blocks are expressions with values ​​in Ruby.

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

+12
source

This is the + method called by the result of the if-else-end .

See below for an example:

 m = if true "abc" else "xyz" end + "mm" # => "abcmm" 
+4
source

Other answers explain how to read. They do not believe that it is not good to imitate.

Adding something to the result of a conditional test could be done using:

 def foo(s) ret = if (s == "something") 'some text' else '' end ret + reference_name end 

This one line is optional, just as fast, but understandable.

Another method works technically and syntactically, but it is not so idiomatic and affects maintenance.

+2
source

Source: https://habr.com/ru/post/1487770/


All Articles