How to Initialize an Array in One Step Using Ruby

How to initialize an array in one step using Ruby?

You can use an array literal:

array = [ '1', '2', '3' ]

You can also use a range:

array = ('1'..'3').to_a  # parentheses are required
# or
array = *('1'..'3') # parentheses not required, but included for clarity

For arrays of whitespace-delimited strings, you can use Percent String syntax:

array = %w[ 1 2 3 ]

You can also pass a block to Array.new to determine what the value for each entry will be:

array = Array.new(3) { |i| (i+1).to_s }

Finally, although it doesn't produce the same array of three strings as the other answers above, note also that you can use enumerators in Ruby 1.8.7+ to create arrays; for example:

array = 1.step(17,3).to_a
#=> [1, 4, 7, 10, 13, 16]

Create array in Ruby with begin value, end value and step for float values

For floats with custom stepping you can use Numeric#step like so:

-1.25.step(by: 0.5, to: 1.25).to_a
# => [-1.25, -0.75, -0.25, 0.25, 0.75, 1.25]

If you are looking on how to do this with integer values only, see this post or that post on how to create ranges and simply call .to_a at the end. Example:

(-1..1).step(0.5).to_a
# => [-1.0, -0.5, 0.0, 0.5, 1.0]

How to initialize all the values of a two dimensional array using Ruby, is there any way to do it in 1 step?

Sure

Array.new(3) { Array.new(4, 0) }
=> [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

How to create array from 1 to n digit with single line of code in ruby

Convert a range into an array.

(1..n).to_a

Fill array with element N times

For immutable objects like Fixnums etc

Array.new(5, 1234) # Assigns the given instance to each item
# => [1234, 1234, 1234, 1234, 1234]

For Mutable objects like String Arrays

Array.new(5) { "Lorem" } # Calls block for each item
# => ["Lorem", "Lorem", "Lorem", "Lorem", "Lorem"]

Create an array with a fixed size, and fill default content with another array?

def fixed_array(size, other)  
Array.new(size) { |i| other[i] }
end
fixed_array(5, [1, 2, 3])
# => [1, 2, 3, nil, nil]

Printing an array of arrays on one line in console (one line per master array object) in Ruby

your_array.each do |person|
puts person.join(" ")
end

Why is it not possible to fill an array using an each do loop in Ruby?

Solution

You need :

array = Array.new(4) do |i|
5
# or some logic depending on i (the index between 0 and 3)
end

Your code

array = Array.new(4)

array is now an Array with 4 elements (nil each time).

array.each iterates over those 4 elements (still nil), and sets i as block-local variable equal to nil.

Inside this block, you override i with 5, but you don't do anything with it. During the next iteration, i is set back to nil, and to 5, and so on...

You don't change the original array, you only change local variables that have been set equal to the array elements.

how to work with Array without changing its initial values?

Your code has the same problems as this would in Python:

edges = [[[1, 2], [100, 200]], [[700, 400], [1000, 2000]]]
new_edges = list(edges)
for edge in edges:
for point in edge:
point.insert(0, 808912)
print(edges)

I.e. you transform the outer array, but the inner arrays are only held by reference, and thus changing one (by insert) is changing the deep content of both edges and new_edges. These kinds of problems are very easily understood by stepping through your code with this tool (despite the name, it works for both Python and Ruby).

In Ruby, instead of using insert, which modifies an array, you can use +, which does not:

edges = [[[1, 2], [100, 200]], [[700, 400], [1000, 2000]]]
new_edges = edges.map { |edge| edge.map { |point| [808912] + point } }
# => [[[808912, 1, 2], [808912, 100, 200]], [[808912, 700, 400], [808912, 1000, 2000]]]
edges
# => [[[1, 2], [100, 200]], [[700, 400], [1000, 2000]]]

Intitalizing Object with Array of objects from another class Ruby

Your problem is assigning a new value to @stars_array variable on each iteration. There are multiple ways to deal with it:

@stars_array = (0..99).map { |i| Star.new('unknown_star',i) }

By the way, there is a couple of design issues (just for your attention):

  1. Why variable is called stars_array, not just stars?

  2. Why would ever instance of Star class have some object named @star inside? Recursion? :) Seems like @name would be proper and more clear attribute's name.

  3. Don't miss indentation.


EDIT: About DB-mapping. Most common way - inherit both classes from ActiveRecord::Base, and create one-to-many relation from solar system to stars. Each class will have it's own table. Takes absolutely no efforts.



Related Topics



Leave a reply



Submit