1

This is one of solution to this problem.

a=[];$<.map{|l|l=='0'?($><<a.pop):a<<l}

I run it on a terminal but it displays no output.

Where has $> which is $DEFAULT_OUTPUT gone?

I used p $> etc but it does not output the result.

How can I display the result of $> on a terminal?

shin
  • 31,901
  • 69
  • 184
  • 271

1 Answers1

1

$> works fine, the problem is elsewhere.

irb> $><<:hello_world
hello_world=> #<IO:<STDOUT>>

The real culprit is $< (which is ARGF). When you run $<.each { } in the terminal, there are no args for ARGF and it hangs waiting for them.

irb> $<.map { }
# hangs...

If you put this in a file and ran it with command line arguments, or piped in the arguments from something else, it would probably do what you expect.

If you replace $< with an array, it seems to work.

arr = ['0', '1', '2']
a=[];arr.map{|l|l=='0'?($><<a.pop):a<<l}
#=> [#<IO:<STDOUT>>, ["1", "2"], ["1", "2"]]
Alex Wayne
  • 178,991
  • 47
  • 309
  • 337