Ruby variable (Array) assignment misunderstanding (with push method)
Ruby works with references! Keep that in mind. If you want a copy you'd have to do it like:
@instance_ar = [1,2,3,4]
local_ar = @instance_ar.clone
local_ar_2 = local_ar.clone
Edit:
Examples:
a = ["a", "b", "c"]
b = a[0]
b = "d" # We assign a new object to b!
a
is:
=> ["a", "b", "c"]
but:
a = ["a", "b", "c"]
b = a[0]
b[0] = "d" # We are working with the reference!
a is:
=> ["d", "b", "c"]
a = "hello"
b = a
b += " world"
# Is the same as b = b + " world", we assign a new object!
a
is:
=> "hello"
but:
a = "hello"
b = a
b<<" world"
# We are working with the reference!
a
is:
=> "hello world"
a = "abc"
b = a
b[0] = "d" # we are working with the reference
a
is:
=> "dbc"
You can read everything about it here: http://ruby-doc.org/docs/ProgrammingRuby/. Scroll down to "Variables" almost at the bottom of the page.
Array asignment error
Because other_array
is @arr
.
......................
Accumulator for each_with_object keeps re-initializing
Each iteration gets the same object, so you either need to mutate the object inside the block, or use reduce
.
def gcp(dims)
first = dims.shift
dims.reduce((0...first).to_a) do |v, dim|
puts "\nv: #{v}, dim: #{dim}"
p v.product((0...dim).to_a)
end
end
gcp([3,2,4])
Results in:
v: [0, 1, 2], dim: 2
[[0, 0], [0, 1], [1, 0], [1, 1], [2, 0], [2, 1]]
v: [[0, 0], [0, 1], [1, 0], [1, 1], [2, 0], [2, 1]], dim: 4
[[[0, 0], 0], [[0, 0], 1], [[0, 0], 2], [[0, 0], 3], [[0, 1], 0], [[0, 1], 1], [[0, 1], 2], [[0, 1], 3], [[1, 0], 0], [[1, 0], 1], [[1, 0], 2], [[1, 0], 3], [[1, 1], 0], [[1, 1], 1], [[1, 1], 2], [[1, 1], 3], [[2, 0], 0], [[2, 0], 1], [[2, 0], 2], [[2, 0], 3], [[2, 1], 0], [[2, 1], 1], [[2, 1], 2], [[2, 1], 3]]
Reassigning Elements of Array: Syntax (Ruby)
The array[0,1]
syntax is picking a slice of the array starting at 0
and of length 1
. Longer slices make that more obvious.
> a = [1,2,3]
=> [1,2,3]
> a[0,2]
=> [1, 2]
To swap the way you want in your first example, you need to specify both indices independently.
> a[0], a[1] = a[1], a[0]
=> [2, 1]
> a
=> [2, 1, 3]
In your second example, Ruby replaces the array[0,1]
slice with [1, 0]
, effectively removing the first element and inserting the new [1, 0]
. Changing to array[0], array[1] = [1, 0]
will fix that for you too.
erb idiom to handle undefined variable
I would do this:
<% if defined?(environment) %>
<% Array(environment).each do |f| %>
one line: <%= f %>
<% end %>
<% end %>
I didn't understand why you joining on new lines and then splitting on them again, so I removed it from the example.
How do I modify an array while I am iterating over it in Ruby?
Use map
to create a new array from the old one:
arr2 = arr.map {|item| item * 3}
Use map!
to modify the array in place:
arr.map! {|item| item * 3}
See it working online: ideone
Cloning an array with its content
You need to do a deep copy of your array.
Here is the way to do it
Marshal.load(Marshal.dump(a))
This is because you are cloning the array but not the elements inside. So the array object is different but the elements it contains are the same instances. You could, for example, also do a.each{|e| b << e.dup}
for your case
In Ruby, is there an Array method that combines 'select' and 'map'?
I usually use map
and compact
together along with my selection criteria as a postfix if
. compact
gets rid of the nils.
jruby-1.5.0 > [1,1,1,2,3,4].map{|n| n*3 if n==1}
=> [3, 3, 3, nil, nil, nil]
jruby-1.5.0 > [1,1,1,2,3,4].map{|n| n*3 if n==1}.compact
=> [3, 3, 3]
Related Topics
How to Insert a String into a Textfile
How to Fire Raw Mongodb Queries Directly in Ruby
How to Traverse Symlinked Directories in Ruby with a "**" Glob
How to Find Each Instance of a Class in Ruby
Converting from Xml Name-Values into Simple Hash
Generate an HTML Table from an Array of Hashes in Ruby
Net::Ssh Sudo Command Hangs After Entering Password
How to Get the Last SQL Query Performed by Activerecord in Ruby on Rails
How Is Each_With_Object Supposed to Work
Rails 3 Install Error: "Invalid Value for @Cert_Chain"
Ruby Timeouts and System Commands
To_Specs': Could Not Find Chef (>= 0) Amongst [] (Gem::Loaderror)
How to Test 'Rand()' with Rspec
How to Write a Ruby Switch Statement (Case...When) with Regex and Backreferences