Ruby array initialize on read - ruby

I would like to be able to do this:
my_array = Array.new
my_array[12] += 1
In other words, somehow upon trying to access entry 12, finding it uninitialized, it is initialized to zero so I can add one to it. Array.new has a default: parameter, but that comes into play when you initialize the array with a known number of slots. Other than writing my own class, is there a ruby-ish way of doing this?

No need to create a new class :
my_hash = Hash.new(0)
my_hash[12] += 1
p my_hash
#=> {12=>1}
For many cases, hashes and arrays can be used interchangeably.
An array with an arbitrary number of elements and a default value sounds like a hash to me ;)
Just to make it clear : Hash and Array aren't equivalent. There will be cases where using a hash instead of an array will be completely wrong.

Something like:
a[12] = (a[12] ||= 0) + 1

Making use of nil.to_i == 0
my_array = Array.new
my_array[12] = my_array[12].to_i + 1

Note, that unlike other solutions here so far, this one works for any arbitrary initial value.
my_array = Array.new.extend(Module.new {
def [] idx
super || 0
end
})
my_array[12] += 1
#⇒ 1

This is not possible with the stock Array::new method.
https://docs.ruby-lang.org/en/2.0.0/Array.html#method-c-new
You will either need to monkey patch Array class, or monkey patch nil class. And they are not recommended.
If you have a specific use case, I would create a new wrapper class around Array
class MyArray < Array
def [](i)
super(i) ? super(i) : self[i] = 0
end
end
arr = MyArray.new
arr[12] += 1 # => 1

Related

Using range.each vs while-loop to work with sequence of numbers in Ruby

Total beginner here, so I apologize if a) this question isn't appropriate or b) I haven't asked it properly.
I'm working on simple practice problems in Ruby and I noticed that while I arrived at a solution that works, when my solution runs in a visualizer, it gives premature returns for the array. Is this problematic? I'm also wondering if there's any reason (stylistically, conceptually, etc.) why you would want to use a while-loop vs. a for-loop with range for a problem like this or fizzbuzz.
Thank you for any help/advice!
The practice problem is:
# Write a method which collects all numbers between small_num and big_num into
an array. Ex: range(2, 5) => [2, 3, 4, 5]
My solution:
def range(small_num, big_num)
arr = []
(small_num..big_num).each do |num|
arr.push(num)
end
return arr
end
The provided solution:
def range(small_num, big_num)
collection = []
i = small_num
while i <= big_num
collection << i
i += 1
end
collection
end
Here's a simplified version of your code:
def range(small_num, big_num)
arr = [ ]
(small_num..big_num).each do |num|
arr << num
end
arr
end
Where the << or push function does technically have a return value, and that return value is the modified array. This is just how Ruby works. Every method must return something even if that something is "nothing" in the form of nil. As with everything in Ruby even nil is an object.
You're not obligated to use the return values, though if you did want to you could. Here's a version with inject:
def range(small_num, big_num)
(small_num..big_num).inject([ ]) do |arr, num|
arr << num
end
end
Where the inject method takes the return value of each block and feeds it in as the "seed" for the next round. As << returns the array this makes it very convenient to chain.
The most minimal version is, of course:
def range(small_num, big_num)
(small_num..big_num).to_a
end
Or as Sagar points out, using the splat operator:
def range(small_num, big_num)
[*small_num..big_num]
end
Where when you splat something you're in effect flattening those values into the array instead of storing them in a sub-array.

Ruby << Hash << Array.include[incremented integer]

I have the following:
ht = Hash.new {|h,k| h[k]=[]}
CertainParentClass.decendants.each do {|childrens|
ht[childrens] << incremented_integer
}
$global_switch[0 ... ht.size] = false
def check_switch(object_class)
return $global_switch[ht[object_class][0]]
end
And I'd like to know how to do incremented_integer in ruby or if there's a better solution rather than inserting an integer to check the position value, like key_position that would be great!
def check_switch(object_class)
return $global_switch[ht[object_class].key_position]
end
I think something below using Enumerable#each_with_index:
CertainParentClass.decendants.each_with_index do |childrens,ind|
ht[childrens] << ind # ind will increment with each pass by 1 default.
end
In case of #each_with_index, ind value will bet set as 0 by default. Now if you want to set it with other numbers rather than 0, use Enumerator#with_index. Like below
CertainParentClass.decendants.each.with_index(2) do |childrens,ind|
ht[childrens] << ind # ind will increment with each pass by 1 default.
end
I passed 2 as initial argument, which is the first ind value in the with_index(2) method, you can change it as per your need
You can use modified iterator :with_index of Enumerable module, which added index number to the end of block argument list, do as follows:
CertainParentClass.decendants.each.with_index do |child, index|
ht[ childrens ] << index
end
And of couse you can modify not only :each iterator method, but a method, which returns modified, or non modified enumerable. Those methods are :map, :reject, :select.

getting dimension of multidimensional array in ruby

I just started learning ruby.
Now I need to figure out the dimension of a multidimensional array. I had a look at ruby-docs for the all the array methods, but I could not find a method that returns the dimension.
Here is an example:
For [[1, 2],[3,4],[5,6]] the dimension should be 2.
For [[[1,2],[2,3]],[[3,4],[5]]], the dimension should be 3.
Simple, object-oriented solution.
class Array
def depth
map {|element| element.depth + 1 }.max
end
end
class Object
def depth
0
end
end
There is not a built-in function for that as there may be multiple definition as to what you mean by "dimension" for an array. Ruby's arrays may contain anything including hashes or other arrays. That's why I believe you need to implement your own function for that.
Asuming that by dimension you mean "the deepest nested level of arrays" this should do the trick:
def get_dimension a
return 0 if a.class != Array
result = 1
a.each do |sub_a|
if sub_a.class == Array
dim = get_dimension(sub_a)
result = dim + 1 if dim + 1 > result
end
end
return result
end
EDIT: and as ruby is a great language and allows you to do some fancy stuff you can also make get_dimension a method of Array:
class Array
def get_dimension
... # code from above slightly modified
end
end
in the simplest case
depth = Proc.new do |array|
depth = 1
while Array === array.first do
array = array.first
depth += 1
end
depth
end
array = [[[1,2],[2,3]],[[3,4],[5]]]
depth.call(array)
#=> 3
Or this tiny recursive method
def depth(array, depth=1)
array = array.send(:first)
Array === array ? depth(array, depth+1) : depth
end
array = [[[1,2],[2,3]],[[3,4],[5]]]
depth(array)
#=> 3
How about:
class Object
def dimension
self.class == Array ? 1 + self[0].dimension : 0
end
end
[[[1,2],[2,3]],[[3,4],[5]]].dimension
#=> 3
As a modification of Tass's approach:
class Array
def depth
map{ |element| element.is_a?( Vector ) ? element.depth + 1 : 1 }.max
end
end
Keeps depth as a method of Array, and doesn't require adding a method to Object.
Of course, that might be what you want if you are going to call my_object.depth, where you don't know in advance that my_object.class == Array
I was not satisfied with the other solutions so I wrote a one-liner I'd actually use:
def depth(array)
array.to_a == array.flatten(1) ? 1 : depth(array.flatten(1)) + 1
end
It will flatten the array 1 dimension at the time until it can't flatten anymore, while counting the dimensions.
Why is this better?
doesn't require modification to native classes (avoid that if possible)
doesn't use metaprogramming (is_a?, send, respond_to?, etc.)
fairly easy to read
works with hashes as well (notice array.to_a)
actually works (unlike only checking the first branch, and other silly stuff)

In Ruby, how can I collect each new element passing through a method into an array?

I'm creating a small prime number program, and am confused about one thing.
I have a function called create_numbers, that generates numbers and passes them to a new function called check_for_primes, which passes only prime numbers to a final function called count_primes. I want to collect each prime into an array in the function count_primes, but for some reason each number is collected as its own array.
Any idea of what I'm doing wrong?
Here is the code:
def create_numbers
nums = 1
while nums < 100
nums = nums + 2
check_for_primes(nums)
end
end
def count_primes(nums)
array = []
array << nums
puts array.inspect
end
def check_for_primes(nums)
(2...nums).each do |i|
if nums%i == 0
nums = false
break
end
end
if nums != false
count_primes(nums)
end
end
create_numbers
Try this:
START = 1
STEP = 2
class Integer
def prime?
return if self < 2
(2...self).each do |i|
return if self % i == 0
end
true
end
end
def create_numbers
num = START
while (num + STEP) < 100
num += STEP
primes << num if num.prime?
end
end
def primes
#primes ||= []
end
create_numbers
p primes
When you want to save the 'state' of something, put it in an instance variable (#var).
It'll be accessible outside of the current function's scope.
Also, try naming your variables differently. For instance, instead of 'nums', in the
create_numbers method, use 'num'. Since the variable is only referencing one number at a
time and not a list of numbers, naming it in the plural will confuse people (me included)...
Hope it helps,
-Luke
each time into count_primes you put a value into array (which should have a better name, btw). Unfortunately, each time it's a new variable called array and since no one outside the function can see that variable it's lost when the function ends. If you want to save the values you've already found you'll need to set some state outside your function.
I can think of 2 quick solutions. One would be to declare your storage at the top of create_numbers and pass it into both functions.
def count_primes(num, arr)
def check_for_primes(nums, arr)
The other would be to set a variable outside all the functions, $array, for example to hold the values.
$array = []
...
$array << num
Since the scope of $array is global (i.e. all functions have access to it) you have access to it from anywhere in the file and can just add things to it in count primes. Note that using globals in this way is generally considered bad style and a more elegant solution would pass parameters and use return values.

How do I dynamically decide which hash to add a value to?

I have a class that has hashes in various stages of "completion". This is to optimize so that I don't have to keep recreating hashes with root data that I already know. For example this is a counter called #root that would serve as a starting point.
{3=>4, 4=>1, 10=>3, 12=>5, 17=>1}
and it took key+key+key+key+key number of iterations to create #root. But now I have all combinations of [x,y] left to be added to the counter and individually evaluated. So I could do it like:
a = (1..52)
a.combination{|x,y|
evaluate(x,y)
}
But instead of I would like to do this:
a.each{|x|
evaluate(x, "foo")
a.each {|y| evaluate(y, "bar")}
}
Where i have a method like this to keep track of the hash at each state:
def evaluate index, hsh
case hsh
when "root"
#root.key?(index) ? #root[index] += 1 : #root[index] = 1
when "foo"
#foo = #root.clone
#foo.key?(index) ? #foo[index] += 1 : #foo[index] = 1
when "bar"
#bar = #foo.clone
#bar.key?(index) ? #bar[index] += 1 : #bar[index] = 1
end
end
But there is alot of repetition in this method. Is there a way that I could do this dynamically without using eval?
Instead of using hsh as a string descriptor, you can directly pass the hash object as parameter to your method evaluate? E.g. instead of evaluate(x, "foo") you write
#foo = #root.clone
evaluate(x, #foo)
Also note the #root.clone in your code overwrites the field several times inside the loop.
Additionally if you use a default initializer for your hash you save quite some logic in your code. E.g. the code lines
h = Hash.new{0}
...
h[index] += 1
will set the default value to zero if non was set for index. Thus you do not have to take care of the special case inside your evaluate method.

Resources