sharipov_ru
sharipov_ru

Reputation: 643

How to initialize objects with different object_id in Ruby?

If I initialize objects with assignment like a = b = c = []

then this variables have the same object_ids: a.object_id == b.object_id == c.object_id

Also I tried:

[a, b, c].map {|e| e = [] }

a, b, c = Array.new(3, [])

a, b, c = Array.new(3, Array.new)

but it doensn't initialize a, b, c variables with different object_ids

Is there a way to initialize variables a, b, c with different object ids but with the same value == []?

Upvotes: 0

Views: 180

Answers (2)

Howard
Howard

Reputation: 39197

How about these possible solutions:

a,b,c=[],[],[]
a,b,c=(0..2).map{[]}
a,b,c=Array.new(3){[]}

Upvotes: 8

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230306

How about this?

a = []
b = []
c = []

Upvotes: 2

Related Questions