Skip to main content
replaced http://codegolf.stackexchange.com/ with https://codegolf.stackexchange.com/
Source Link

When a challenge requires that you output multiple lines, you don't have to looploop through your results in order to print each line of e.g. an array. The puts method will flatten an array and print each element on a separate line.

> a = %w(testing one two three)
> puts a
testing
one
two
three

Combining the splat operator with #p#p you can make it even shorter:

p *a

The splat operator (technically the *@ method, I think) also casts your non-array enumerables to arrays:

> p a.lazy.map{|x|x*2}
#<Enumerator::Lazy: #<Enumerator::Lazy: [1, 2, 3]>:map>

vs

> p *a.lazy.map{|x|x*2}
2
4
6

When a challenge requires that you output multiple lines, you don't have to loop through your results in order to print each line of e.g. an array. The puts method will flatten an array and print each element on a separate line.

> a = %w(testing one two three)
> puts a
testing
one
two
three

Combining the splat operator with #p you can make it even shorter:

p *a

The splat operator (technically the *@ method, I think) also casts your non-array enumerables to arrays:

> p a.lazy.map{|x|x*2}
#<Enumerator::Lazy: #<Enumerator::Lazy: [1, 2, 3]>:map>

vs

> p *a.lazy.map{|x|x*2}
2
4
6

When a challenge requires that you output multiple lines, you don't have to loop through your results in order to print each line of e.g. an array. The puts method will flatten an array and print each element on a separate line.

> a = %w(testing one two three)
> puts a
testing
one
two
three

Combining the splat operator with #p you can make it even shorter:

p *a

The splat operator (technically the *@ method, I think) also casts your non-array enumerables to arrays:

> p a.lazy.map{|x|x*2}
#<Enumerator::Lazy: #<Enumerator::Lazy: [1, 2, 3]>:map>

vs

> p *a.lazy.map{|x|x*2}
2
4
6
Source Link
daniero
  • 18.7k
  • 5
  • 51
  • 78

When a challenge requires that you output multiple lines, you don't have to loop through your results in order to print each line of e.g. an array. The puts method will flatten an array and print each element on a separate line.

> a = %w(testing one two three)
> puts a
testing
one
two
three

Combining the splat operator with #p you can make it even shorter:

p *a

The splat operator (technically the *@ method, I think) also casts your non-array enumerables to arrays:

> p a.lazy.map{|x|x*2}
#<Enumerator::Lazy: #<Enumerator::Lazy: [1, 2, 3]>:map>

vs

> p *a.lazy.map{|x|x*2}
2
4
6