Related
I'm pretty good at getting answers from google, but I just don't get this. In the following code, why does variable 'b' get changed after calling 'addup'? I think I understand why 'a' gets changed (although its a bit fuzzy), but I want to save the original array 'a' into 'b', run the method on 'a' so I have two arrays with different content. What am I doing wrong?
Thanks in advance
def addup(arr)
i=0
while i< arr.length
if arr[i]>3
arr.delete_at(i)
end
i += 1
end
return arr
end
a = [1,2,3,4]
b = a
puts "a=#{a}" # => [1,2,3,4]
puts "b=#{b}" # => [1,2,3,4]
puts "addup=#{addup(a)}" # => [1,2,3]
puts "a=#{a}" # => [1,2,3]
puts "b=#{b}" # => [1,2,3]
Both a and b hold a reference to the same array object in memory. In order to save the original array in b, you'd need to copy the array.
a = [1,2,3,4] # => [1, 2, 3, 4]
b = a # => [1, 2, 3, 4]
c = a.dup # => [1, 2, 3, 4]
a.push 5 # => [1, 2, 3, 4, 5]
a # => [1, 2, 3, 4, 5]
b # => [1, 2, 3, 4, 5]
c # => [1, 2, 3, 4]
For more information on why this is happening, read Is Ruby pass by reference or by value?
but I want to save the original array 'a' into 'b'
You are not saving the original array into b. Value of a is a reference to an array. You are copying a reference, which still points to the same array. No matter which reference you use to mutate the array, the changes will be visible through both references, because, again, they point to the same array.
To get a copy of the array, you have to explicitly do that. For shallow arrays with primitive values, simple a.dup will suffice. For structures which are nested or contain references to complex objects, you likely need a deep copy. Something like this:
b = Marhal.load(Marshal.dump(a))
In the following code, why does variable 'b' get changed after calling 'addup'?
The variable doesn't get changed. It still references the exact same array it did before.
There are only two ways to change a variable in Ruby:
Assignment (foo = :bar)
Reflection (Binding#local_variable_set, Object#instance_variable_set, Module#class_variable_set, Module#const_set)
Neither of those is used here.
I think I understand why 'a' gets changed (although its a bit fuzzy)
a doesn't get changed either. It also still references the exact same array it did before. (Which, incidentally, is the same array that b references.)
The only thing which does change is the internal state of the array that is referenced by both a and b. So, if you really understand why the array referenced by a changes, then you also understand why the array referenced by b changes, since it is the same array. There is only one array in your code.
The immediate problem with your code is that, if you want a copy of the array, then you need to actually make a copy of the array. That's what Object#dup and Object#clone are for:
b = a.clone
Will fix your code.
BUT!
There are some other problems in your code. The main problem is mutation. If at all possible, you should avoid mutation (and side-effects in general, of which mutation is only one example) as much as possible and only use it when you really, REALLY have to. In particular, you should never mutate objects you don't own, and this means you should never mutate objects that were passed to you as arguments.
However, in your addup method, you mutate the array that is passed to you as arr. Mutation is the source of your problem, if you didn't mutate arr but instead returned a new array with the modifications you want, then you wouldn't have the problem in the first place. One way of not mutating the argument would be to move the cloneing into the method, but there is an even better way.
Another problem with your code is that you are using a loop. In Ruby, there is almost never a situation where a loop is the best solution. In fact, I would go so far as to argue that if you are using a loop, you are doing it wrong.
Loops are error-prone, hard to understand, hard to get right, and they depend on side-effects. A loop cannot work without side-effects, yet, we just said we want to avoid side-effects!
Case in point: your loop contains a serious bug. If I pass [1, 2, 3, 4, 5], the result will be [1, 2, 3, 5]. Why? Because of mutation and manual looping:
In the fourth iteration of the loop, at the beginning, the array looks like this:
[1, 2, 3, 4, 5]
# ↑
# i
After the call to delete_at(i), the array looks like this:
[1, 2, 3, 5]
# ↑
# i
Now, you increment i, so the situation looks like this:
[1, 2, 3, 5]
# ↑
# i
i is now greater than the length of the array, ergo, the loop ends, and the 5 never gets removed.
What you really want, is this:
def addup(arr)
arr.reject {|el| el > 3 }
end
a = [1, 2, 3, 4, 5]
b = a
puts "a=#{a}" # => [1, 2, 3, 4, 5]
puts "b=#{b}" # => [1, 2, 3, 4, 5]
puts "addup=#{addup(a)}" # => [1, 2, 3]
puts "a=#{a}" # => [1, 2, 3, 4, 5]
puts "b=#{b}" # => [1, 2, 3, 4, 5]
As you can see, nothing was mutated. addup simply returns the new array with the modifications you want. If you want to refer to that array later, you can assign it to a variable:
c = addup(a)
There is no need to manually fiddle with loop indices. There is no need to copy or clone anything. There is no "spooky action at a distance", as Albert Einstein called it. We fixed two bugs and removed 7 lines of code, simply by
avoiding mutation
avoiding loops
I'm a new Ruby programmer performing an exercise to make a program that lets people play fish. I'm building a method that will search for books and handle them when it finds them. I have my player hands stored in an array, so I want to search them for matches.
My 2 questions are:
What is the easiest, most effective and simplest way to search
through an array?
When I find 4 of a certain element in the same array, what is the
best way to delete those elements from the array?
I need to:
SEARCH the array for each element of %w(2 3 4 5 6 7 8 9 10 J Q K A)
#Values for cards, (I'm going to use a do each iterator for this, I got that). I need to know how to search the array in the first place.
Count each time I come across a match for the element
If count == 4, I have a book so I need to know how to delete specific elements of the array #(the 4 matching cards)
I know this is simple stuff, so thanks for the help and patience!
To search for elements in an array you can make use of select (alias find_all). Select will loop through and "select" elements that match conditions in the provided block, returning "selected" elements in an array.
Example:
['2', '3', '4'].select { |card| card == '2' } #=> ["2"]
You could also remove the need to search through an array by using a hash instead (mapping card values to the number of cards). This would allow you to easily check how many cards of a certain type a hand contains (see below for an example).
To count each time you come across an element you will need some data structure to store a card value and a count. A hash would be a good option here.
Example: card value => count
{ '2' => 1 }
To delete elements from an array you can use #delete or #delete_at to delete elements based on value or elements based on their array index. Another option is to use bang methods like #select! to transform your current array based on a provided block. Ruby has plenty of options for tasks like this.
I'd recommend skimming through methods for Array and Enumerable to get a better handle on what can be done with arrays. It's well worth your time as a lot of Ruby's power and elegance lies with Array and Enumerable.
http://www.ruby-doc.org/core-2.0.0/Array.html#method-i-select
http://ruby-doc.org/core-2.0.0/Enumerable.html#method-i-select
Best of luck!
Lots of overly complex answers.
Want to know if an element is in an array? Use include?.
[1, 2, 3, 4].include?(4) # true
[1, 2, 3, 4].include?(5) # false
Want to delete all instances of a value from an array? Use delete
x = [1, 2, 3, 4]
x.delete(4) # 4
x # [1, 2, 3]
y = [1, 1, 2, 2, 3, 3]
y.delete(4) # nil
y # [1, 1, 2, 2, 3, 3]
y.delete(2) # 2
y # [1, 1, 3, 3]
If you want to count the number of times a term appears, use count:
x = ['a', 'a', 'b', 'b', 'b']
x.count('a') # 2
x.count('b') # 3
You might want to have a look at enumerable module. There is pretty neat documentation here: http://ruby-doc.org/core-2.0.0/Enumerable.html My advise is to scan all the methods in this module as they are all extremely useful. For your task I would probably use count method. If you want to count all cards, group_by is what you want. Together with map you can do:
hand = [2,3,6,10,2,J,A,3,J,2,K,K,2]
Hash[hand.group_by{|value| value}.map{|key, values| [key, values.count]}]
# => {2 => 4, 3 => 2, 6 => 1, 10 => 1, J => 2, A => 1, K => 2}
I have created several arrays, containing multiple integers. Now i want the integers to be sorted, lowest first. Say for instance, i have this in an array: 6,6,1,2,4,4, i want it to be sorted: 1,2,4,4,6,6. Also, is there anyway i can make ruby recognize the 4 lowest values, and display them somehow? I have tried to mess around with .show, but since im quite new to programming i'm rather confused by the results i receive.
did you try this?
a = [6,6,1,2,4,4]
p a.sort
#=> [1, 2, 4, 4, 6, 6]
sort will sort in ascending order.
if you need them sorted in descending order, use sort with a block:
p a.sort {|a,b| b <=> a}
#=> [6, 6, 4, 4, 2, 1]
UPDATE: not sure how i missed the part about lowest values ...
thank you #Mladen
a.sort.take(4)
#=> [1, 2, 4, 4]
I'm new to Ruby (and programming in general) and have been reading a ton of docs, how-tos and SO questions to try to find an answer to this problem but no luck so far.
I have an array of integers and I'm trying to save one of the object's integers to a variable to later delete that object from the array. What I have so far:
array = [3, 5, 1, 2, 6, 9]
objToDel = array[3]
array.delete_at(objToDel)
array
This deletes "1" in the array... I want it to delete "2" instead. I know this happens because having the variable point to array[3] points it to "2" instead of the actual third element in the array. I have tried the slice method as well to no avail.
So, is it possible to get a variable to equal an element's index instead of its content? Is this possible without turning the array into a hash?
Thanks in advance!
Sure, just assign the index to the variable:
index = 3
array.delete_at(index) # => 2
array # => [3, 5, 1, 6, 9]
You could also use the delete method to remove the object directly.
object_to_delete = 2
array.delete(object_to_delete) # => 2
array # => [3, 5, 1, 6, 9]
Note that this deletes all instances of the object in the array, which might not be what you want.
To keep it in your words, try this:
array = [3, 5, 1, 2, 6, 9]
objToDel = 3
array.delete_at(objToDel)
array
Good luck.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Ruby - What is the difference between map, each and collect?
I have looked in Ruby-Doc also but i cant understand the difference between
map
each
iterators.It would be great if you could give an example and explain.
each simply iterates over the given enumerable, running the block for each value. It discards the return value of the block, and each simply returns the original object it was called on:
[1, 2, 3].each do |x|
x + 1
end # => [1, 2, 3]
This is simply a nicer, more universal way of doing a traditional iterating for loop, and each is much preferred over for loops in Ruby (in fact, I don't think I've ever used a for loop in Ruby).
map, however, iterates over each element, using the return value of the block to populate a new array at each respective index and return that new array:
[1, 2, 3].map do |x|
x + 1
end # => [2, 3, 4]
So it “maps” each element to a new one using the block given, hence the name “map”. Note that neither each nor map themselves modify the original collection. This is a concise, functional alternative to creating an array and pushing to it in an iterative loop.
each returns the original object. It's used to run an operation using each element of an array without collecting any of the results. For example, if you want to print a list of numbers, you might do something like this:
arr = [1, 2, 3, 4]
arr.each { |n| puts n }
Now, that puts method above actually returns nil. Some people don't know that, but it doesn't matter much anyway; there's no real reason to collect that value (if you wanted to convert arr to strings, you should be using arr.map(&:to_s) or arr.map { |n| n.to_s }.
map returns the results of the block you pass to it. It's a great way to run an operation on each element in an array and retrieve the results. If you wanted to multiple every element of an array by 2, this is the natural choice. As a bonus, you can modify the original object using map!. For example:
arr = [1, 2, 3, 4]
arr.map! { |n| n * 2}
# => [2, 4, 6, 8]