Question: How can I shorten this class method (currently 10 lines long)?

```
class DoubleSeries
  def self.generate_sequences(seq1_start, seq2_start, seq1_step, seq2_step, iterations)
    seq1,seq2=seq1_start,seq2_start
    result=[]
    (1..iterations).each do
      result << seq1 << seq2
      seq1+=seq1_step
      seq2+=seq2_step
    end 
    result
  end 
end

```

I tried doing:

```
class DoubleSeries
  def self.generate_sequences(seq1_start, seq2_start, seq1_step, seq2_step, iterations)
    seq1,seq2=seq1_start,seq2_start
    result=
    (1..iterations).each do
      self << seq1 << seq2
      seq1+=seq1_step
      seq2+=seq2_step
    end 
    result
  end 
end

```

but `self` is the class at this point (as this is a class method), not the `result=` variable.

Answer: 
You can do it all in one expression. It's quite long, but the names of the variables give a good hint about the purpose of each part of the code.

```
class DoubleSeries
  def self.generate_sequences(seq1_start, seq2_start, seq1_step, seq2_step, iterations)
    [[seq1_start, seq1_step], [seq2_start, seq2_step]].map do |start, step|
      (0...iterations).map { |i| start + i * step }
    end.inject do |seq1, seq2|
      seq1.zip(seq2)
    end.flatten
  end
end

```
To generate a sequence, the shortest thing i can think of is : 

```
(seq_start..Float::INFINITY).step(seq_step).take(iterations)

```

*note : works under ruby 2, don't know for other versions* 

**EDIT :** warning - unfortunately, this will only work with *positive* steps. 

I think it would be better to change the method's interface to this :

```
def self.generate_sequences( iterations, sequence_options )
  sequences = sequence_options.map do |start,step|
    (start..Float::INFINITY).step(step).take(iterations) 
  end
  first = sequences.shift
  sequences.empty? ? first : first.zip(*sequences).flatten  
end

```

this allows you to pass in an arbitrary number of sequences as a hash :

```
 >> DoubleSeries.generate_sequences(3, 5 => 10, 3 => 3)
 => [5.0, 3.0, 15.0, 6.0, 25.0, 9.0]

```

*note : if your DoubleSeries class has no state and is not meant to be instantiated, you should make it a Module instead (Service object)*