I would appreciate it if anyone could explain why the output of this code is:
*a, b = [1, 2, 3, 4] a[b-2] + b
there is 7. Can someone break it line by line so that I understand what is going on here? How is it getting 7?
7
To break something line by line, you can use REPL:
*a, b = [1, 2, 3, 4] #⇒ [1, 2, 3, 4] a #⇒ [1, 2, 3] b #⇒ 4
Using the splat operator, we decompose the original array into a new array and one value. Now everything is crystal clear: a[b-2]which a[2], which, in turn, 3(check the aarray.), But b still 4.
a[b-2]
a[2]
3
a
4
3 + 4 #⇒ 7
*a, b = [1, 2, 3, 4]it means a = [1,2,3]and b = 4when you perform a[b-2] + bit will
*a, b = [1, 2, 3, 4]
a = [1,2,3]
b = 4
a[b-2] + b
+-----------------------+ a[b-2] + b | a[2] | a[4-2] + 4 | | | a[2] + 4 | a[1, 2, 3] | 3 + 4 | 0 1 2 -> index | = 7 +-----------------------+
,
, a[b-2] + b
a[b - 2] = 3 b = 4 3 + 4 = 7
rails .
*a, b = [1, 2, 3, 4] a => [1, 2, 3] b => 4 a[b-2] // b-2 = 2 find value at index of 2 in a, like a[2] is 3 => 3 a[b-2]+b => 7
Source: https://habr.com/ru/post/1680765/More articles:https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1680760/how-can-i-opt-for-react-styled-components-to-generate-a-physical-css-file&usg=ALkJrhgd5sOgA-jiJ0wF5dTXFC99oeVmLwZeppelin [0.7.2]: NullPointerException when executing a paragraph from a new laptop - apache-sparkHow to get the given array values - arraysCollision name with implementation interface - javaGeneric interface extension no longer assigned to parents - genericsDelete node s child and redraw jstree inside selected node - javascriptHow to enter configuration parameters into test classes (ASP.NET Core)? - c #How to remove child nodes of selected node in jstree? - javascriptError: RuntimeException in DisplayListCanvas.throwIfCannotDraw - javaWhat is the difference between JSONiq and XQuery 3.1? - jsonAll Articles