Creating an array of numbers from 0 to n for positive and negative n

Given nI want to create an array from 0to n:

10.make_array #=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

It should also work if nnegative:

-10.make_array #=> [0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10]

I wrote this code, but I think I made it more complicated than necessary (it does not work for negative numbers, for now):

class Fixnum
  define_method(:make_array) do
    my_array = []
    self.times() do |count|
      self.>(0)
      my_array.push(count)
    end
    my_array.push(self)
    my_array
  end
end

Is there an easier way or short way to do the same and any suggestions on how to handle a negative number?

+4
source share
7 answers

To maintain the negative numbers, you can use uptoand downtowith the condition:

(I am showing here a standalone method instead of fixing it Integer)

def make_array(n)
  if n > 0
    0.upto(n).to_a
  else
    0.downto(n).to_a
  end
end

, Enumerable#to_a.

, (), Array::new - :

n = 3
Array.new(n) #=> [nil, nil, nil]

, , . , , :

Array.new(n) { |i| i } #=> [0, 1, 2]

, Array.new(n) n , n + 1, :

Array.new(n + 1) { |i| i } #=> [0, 1, 2, 3]

, Array::new :

Array.new(-3) #=> negative array size (ArgumentError)

, n -n, -i:

n = -3
Array.new(-n + 1) { |i| -i } #=> [0, -1, -2, -3]

:

def make_array(n)
  if n > 0
    Array.new(n + 1) { |i| i }
  else
    Array.new(-n + 1) { |i| -i }
  end
end

.

n abs:

3.abs  #=> 3
-3.abs #=> 3

:

n = 3
Array.new(n.abs + 1) { |i| i } #=> [0, 1, 2, 3]

n = -3
Array.new(n.abs + 1) { |i| i } #=> [0, 1, 2, 3]

if:

n = 3
Array.new(n.abs + 1) { |i| n > 0 ? i : -i } #=> [0, 1, 2, 3]

n = -3
Array.new(n.abs + 1) { |i| n > 0 ? i : -i } #=> [0, -1, -2, -3]

, a <=> b. , a, b, -1, 0 +1 .

, Fixnum#<=>, n <=> 0 1, n , 0, -1, n 0:

3 <=> 0  #=> 1
-3 <=> 0 #=> -1

<=>, i :

Array.new(n.abs + 1) { |i| i * (n <=> 0) }

i * 1 (if n > 0) i * -1 ( n < 0).

( : n <=> 0 0, n 0, , [0] 0 * 0 0)

:

def make_array(n)
  Array.new(n.abs + 1) { |i| i * (n <=> 0) }
end

, , , . (, upto downto) - .

+1

Range:

(0 .. 10).to_a
#=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

:

(-10 .. -1).to_a
#=> [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1]
+2

Ruby Array.

Array.new(11) {|i| i} #=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

- , , , .

:

Array.new(10) #=> [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]

Array.new(3) {5} #=> [5, 5, 5]

Array.new(8) {|i| i * 2} #=> [0, 2, 4, 6, 8, 10, 12, 14]

: http://ruby-doc.org/core-2.2.0/Array.html#class-Array-label-Creating+Arrays

+1

: [*1..10]

+1

Fixnum

class Fixnum
  define_method(:make_array) do
    Array.new(self + 1) {|i| i}
  end
end

p 3.make_array      # => [0, 1, 2, 3]

, -1, .

After you have noticed editing the saw regarding the processing of negative numbers, follow these steps:

class Fixnum
  define_method(:make_array) do
    self < 0 ? Array.new(-self + 1) {|i| -i} : Array.new(self + 1) {|i| i}
  end
end

-5.make_array     # => [0, -1, -2, -3, -4, -5]
+1
source
class Fixnum
  def sequence_from_zero
    0.step(self, self/self.abs).to_a
  end
end

p 10.sequence_from_zero  # => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
p -10.sequence_from_zero # => [0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10]
+1
source

You can also use Fixnum#upto:

$ ruby -e "p 1.upto(10).to_a"
# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
0
source

Source: https://habr.com/ru/post/1589257/


All Articles