-5

For example, I want to implement this code in IRB, but it has single-line input so I can get how to write block there.

a = [3, 2, 1]
a[3] = a[2] - 1

a.each do |elt|
  print elt+1
end
Billy Logan
  • 2,470
  • 6
  • 27
  • 45
  • You obviously haven't tried doing what you're asking. IRB is an interactive Ruby, and yes, it's line-oriented, but it's plenty smart to recognize your input as the beginning of a block. – the Tin Man Sep 10 '15 at 17:10

1 Answers1

2

(Oh you mean IRB)

If you enter something that will be on multiple lines, ruby will wait until the final end is completed before running the code:

irb(main):001:0> def dostuff
irb(main):002:1>   puts "things"
irb(main):003:1> end
=> :dostuff
irb(main):004:0> dostuff
things
=> nil
irb(main):005:0> 

As you can see the number at the prompt changes depending on how deep the block-level is.

Will Richardson
  • 7,780
  • 7
  • 42
  • 56