How can I get my array to only be manipulated locally (within a function) in Ruby?

supercuteboy

Why is my array globally manipulated, when I run the below ruby code? And how can I get arrays to be manipulated only within the function's scope?

a = [[1,0],[1,1]]

def hasRowsWithOnlyOnes(array)
  array.map { |row|
    return true if row.keep_if{|i| i != 1 } == []
  }
  false;
end

puts a.to_s
puts hasRowsWithOnlyOnes(a)
puts a.to_s

$ ruby test.rb output:

[[1, 0], [1, 1]]
true
[[0], []]

I can't get it to work. I've even tried .select{true} and assign it to a new name. How does the scope work in Ruby for Arrays? Just for reference, $ ruby -v:

ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-linux]
Wayne Conrad

It's not a scope problem, it's an argument passing problem

  • All variables in Ruby are references to objects.

  • When you pass an object to a method, a copy of that object's reference is made and passed to the object.

That means that the variable array in your method and the top-level variable a refer to the exact same Array. Any changes made to array will be also visible as changes to a, since both variables refer to the same object.

Your method does modify the array by calling Array#keep_if. The keep_if method modifies the array in-place.

The fix

The best fix for this is to make it so that your method does not modify the array that was passed in. This can be done pretty neatly using the Enumerable#any? and Enumerable#all? methods:

def has_a_row_with_only_ones(array)
  array.any? do |row|
    row.all? { |e| e == 1 }
  end
end

This code says that the method returns true if, for any row, every element in that row is 1. These methods do not modify the array. More important, they communicate the method's intent clearly.

The poor workaround

If you want the method to act as through a copy of the array were passed to it, so that the array can be modified without that modification being visible outside the method, then you can make a deep copy of the array. As shown in this answer, you can define this method to make a deep copy:

def deep_copy(o)
  Marshal.load(Marshal.dump(o))
end

Then, at the top of the method, make the deep copy:

def has_a_row_with_only_ones(array)
  array = deep_copy(array)
  # code that modifies array
end

This should be avoided because it's slow.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How can I get a list of locally installed Python modules?

From Dev

How can I get the length of my filtered ngRepeat array in Angular?

From Dev

How can I call a function within a website?

From Dev

How can I put an array of hashes, within a hash in Ruby?

From Dev

JSDOM in nodeJS: How do I get back the manipulated html?

From Dev

How can i get value of array using ajax function?

From Dev

How can I get a specific value from an array for my object?

From Dev

How can I get the key Values in a JSON array in ruby?

From Dev

How to get the value of radio buttons so that the value can be manipulated

From Dev

How can I get Jackson to deserialize into my own Array implementation

From Dev

How can I rewrite my removeString function with only one argument?

From Dev

How can i get only my messages from firestore in Flutter

From Dev

Can I backup my IMAP Gmail account locally using only Alpine?

From Dev

How can I get informations within code about the cache of my app?

From Dev

How can I put an array of hashes, within a hash in Ruby?

From Dev

How can I use sudo within a function?

From Dev

how can I get my php array data to persist?

From Dev

how can I get char array size in function (language c)?

From Dev

How can I push element in an array whenever a function get called?

From Dev

how can I get gpg to list only my keys?

From Dev

How to sum a manipulated array in Excel?

From Dev

How to get array size within function?

From Dev

How can I get my Chart.JS to center within a div?

From Dev

RUBY: How can I print only the words that start with a certain letter from an Array?

From Dev

how can i dynamically update my nested object within another object, within an array?

From Dev

How can I get all my contacts phone numbers into an array?

From Dev

Can the req object be manipulated within a request

From Dev

I can't get my locally stored variable value in my Angular 2

From Dev

How can I randomize the values within my numpy array?

Related Related

  1. 1

    How can I get a list of locally installed Python modules?

  2. 2

    How can I get the length of my filtered ngRepeat array in Angular?

  3. 3

    How can I call a function within a website?

  4. 4

    How can I put an array of hashes, within a hash in Ruby?

  5. 5

    JSDOM in nodeJS: How do I get back the manipulated html?

  6. 6

    How can i get value of array using ajax function?

  7. 7

    How can I get a specific value from an array for my object?

  8. 8

    How can I get the key Values in a JSON array in ruby?

  9. 9

    How to get the value of radio buttons so that the value can be manipulated

  10. 10

    How can I get Jackson to deserialize into my own Array implementation

  11. 11

    How can I rewrite my removeString function with only one argument?

  12. 12

    How can i get only my messages from firestore in Flutter

  13. 13

    Can I backup my IMAP Gmail account locally using only Alpine?

  14. 14

    How can I get informations within code about the cache of my app?

  15. 15

    How can I put an array of hashes, within a hash in Ruby?

  16. 16

    How can I use sudo within a function?

  17. 17

    how can I get my php array data to persist?

  18. 18

    how can I get char array size in function (language c)?

  19. 19

    How can I push element in an array whenever a function get called?

  20. 20

    how can I get gpg to list only my keys?

  21. 21

    How to sum a manipulated array in Excel?

  22. 22

    How to get array size within function?

  23. 23

    How can I get my Chart.JS to center within a div?

  24. 24

    RUBY: How can I print only the words that start with a certain letter from an Array?

  25. 25

    how can i dynamically update my nested object within another object, within an array?

  26. 26

    How can I get all my contacts phone numbers into an array?

  27. 27

    Can the req object be manipulated within a request

  28. 28

    I can't get my locally stored variable value in my Angular 2

  29. 29

    How can I randomize the values within my numpy array?

HotTag

Archive