What does "(test1, test2, test3 =" 3 ", test4 =" 4 ", test5 =" 5 ", test6 =" 6 ")" do?

This question is based on some really strange code, which I recently found in the work of colleagues. He claims that he does not know how this works, only he copied it from another place. This is not very good for me, I want to understand what is happening here.

If we have something like:

(test1, test2, test3="3", test4="4")

The result will be such that test1 == "3", test2 == "4", test3 == niland test4 == "4". I understand why this is happening, but if we do something like:

(test1, test2, test3="3", test4="4", test5 = "5", test6 = "6")

Now the result of test1 == "3", test2 == "4", test3 == "5", test4 == "4", test5 == "5", test6 == "6".

Why not test5 == nil?

+3
source share
3

, :

(test1, test2, test3) = ("3"), (test4 = "4"), (test5 = "5"), (test6 = "6")

# Equivalent:
test1 = "3"
test2 = test4 = "4"
test3 = test5 = "5"
      ; test6 = "6"
+3

RHS ( ), , a = b = 4 a, b 4:

a = b = 4
-> a = (b = 4) // Has the "side effect" of setting b to 4
-> a = 4       // a is now set to the result of (b = 4)

, , Ruby , (Ruby , , , LHS ( ) RHS):

test1, test2, test3="3", test4="4", test5 = "5", test6 = "6"
-> test1, test2, test3 = "3", (test4 = "4"), (test5 = "5"), (test6 = "6")

RHS, :

test1, test2, test3 = "3", "4", "5", "6"

test4 "4", test5 "5" test6 "6".

LHS :

test1 = "3"
test2 = "4"
test3 = "5"
// since there are 3 items on the LHS and 4 on the RHS, nothing is assigned to "6"

, :

test1 == "3"
test2 == "4"
test3 == "5"
test4 == "4"
test5 == "5"
test6 == "6"
+2

:

(test1, test2, test3="3", test4="4", test5 = "5", test6 = "6")

, :

test1=="3", test2=="4", test3=="5", test4=="4", test5=="5", test6=="6"

( , test4 "4", "6" )

, :

((test1, test2, test3) = ("3", (test4="4", (test5 = "5", (test6 = "6")))))

So you get an estimate something like this:

((test1, test2, test3) = ("3", (test4="4", (test5 = "5", (test6 = "6")))))
[assign "6" to test6]
((test1, test2, test3) = ("3", (test4="4", (test5 = "5", "6"))))
[assign "5" to test5]
((test1, test2, test3) = ("3", (test4="4", "5", "6")))
[assign "4" to test4]
((test1, test2, test3) = ("3", "4", "5", "6"))
[assign "3", "4", and "5" to test1, test2, and test3 respectively]
+1
source

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


All Articles