Grammar in Ruby "&& ="

What does &&= mean in the following method?

 records.each do |record| raise_on_type_mismatch!(record) add_to_target(record) do |rec| result &&= insert_record(rec, true, should_raise) unless owner. new_record? end end 
+5
source share
2 answers

a &&= b - short form (aka syntactic sugar) for:

 a && a = b 

This short form is provided for operators (including but not limited to) + , - , / , * , % , ** , ^ , << , >> , & , | , && , || (@ Stefan credits for a complete list of supported operator shortcuts ):

 a = 5 a += 5 #⇒ 10 a = true a &&= true #⇒ true a &&= false #⇒ false 

&& is logical and aka " conjunction " unlike || which is logical or aka " disjunction .

+7
source

This means that the previous result value is false or nil , then save it and skip further calls to insert_record , otherwise set the result value to the return value of insert_record .

+3
source

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


All Articles