Assignment syntax where several variables get assigned in parallel.
Questions tagged [parallel-assignment]
11 questions
7
votes
2 answers
swift: if-let with parallel assignment
I'm trying to make a simple if-let statement with more than one value. The if block should be executed only if all optional vars are non-nil, plus they should be assigned to the new let-vars (constants?) that live only inside the if block, just like…

maltalef
- 1,507
- 2
- 16
- 27
5
votes
1 answer
Parallel assignment with parentheses and splat operator
I got this:
x,(y,z)=1,*[2,3]
x # => 1
y # => 2
z # => nil
I want to know why z has the value nil.

Devashish Bhardwaj
- 61
- 3
4
votes
3 answers
Swapping array elements using parallel assignment
Intrigued by this question, I have played a bit with parallel assignment with arrays and method calls. So here's an paradigmatic example, trying to swap two members in an array, by their value:
deck = ['A', 'B', 'C']
#=> ["A", "B",…

Mladen Jablanović
- 43,461
- 10
- 90
- 113
3
votes
4 answers
Why does parallel assignment of a single empty array assign multiple nils?
I want to assign an empty array to multiple variables. Here is what I'm doing:
irb(main):015:0> a, b, c = []
=> []
irb(main):016:0> a
=> nil
irb(main):017:0> b
=> nil
irb(main):018:0> c
=> nil
It gives me nil. I wonder why? But if I did…

Alan Coromano
- 24,958
- 53
- 135
- 205
2
votes
1 answer
Parallel assignment in ruby working differently for two equivalent code fragments
The two code fragments below should print the same thing, but they don't.
ary = %W(1 2 5 6 B 8 5 4 6 5 6 9 7 A)
indx1 = 0...ary.index("B")
indx2 = (ary.index("A") + 1)..-1
ary[indx1], ary[indx2] = ary[indx2], ary[indx1]
puts ary.inspect
ary =…

RaouL
- 1,133
- 4
- 14
- 17
1
vote
2 answers
Reassigning Elements of Array: Syntax (Ruby)
I'm learning the parallel assignment operator in Ruby now. When I tried using it to swap values in an array, I got unexpected results. Couldn't find the answer to this online and was hoping someone can shed the light on what is happening here.
First…

Petr Gazarov
- 3,602
- 2
- 20
- 37
0
votes
2 answers
Ruby: Hash assignment with concurrent loops
Say I have an array:
array = [6, 6, 6, 4, 4, 6, 4, 4]
and I have another array of strings:
quadrant = ["upper_left", "upper_right", "lower_left", "lower_right"]
and I have a 8 x 8 2-d array consisting of board locations(@board) that are either nil…

steve_gallagher
- 3,778
- 8
- 33
- 51
0
votes
1 answer
Iterating over list of hashes via parallel assignment
I've just seen this code (simplified):
def something
yield(key1: 1, key2: 2)
end
something { |key1: key1, key2: key2| puts "key1:#{key1}, key2:#{key2}" }
# (irb):351: warning: circular argument reference - key1
# (irb):351: warning: circular…

estani
- 24,254
- 2
- 93
- 76
0
votes
1 answer
Assign Subset of Variables a Common Value via Parallel Assignment
Within Ruby, I've been searching for an elegant single-line statement to assign multiple variables (but not all) a common value returned from an array via method call.
The crux of the objective is to convert a two-line statement such as:
a, x = 1,…

Cam
- 921
- 7
- 13
0
votes
1 answer
How to create multiple agents in parallel (for? or map?) with name and values in CLOJURE?
I'm trying to make a bunch of agents. Individually, one can do:
(def myAgent (agent 3))
But if I want to make a lot of agents, how can I assign both names and values to the agent in an anonymous function? I have this:
(def agents (vec (map agent…

ProGrammar
- 298
- 4
- 17
0
votes
2 answers
Ruby equilibrium index - odd usage of inject
I spent a bit of time today tackling the equilibrium index problem (described here)
After writing my own solution (which performed badly with large numbers), I decided to find one that would be a perfect score. I found this (which Codility scores as…

Stu
- 154
- 1
- 11