Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

irb does not print anything

Tags:

ruby

I'm a newbie at ruby.
While I am using irb, something happens.(Nothings are printed)
Does anyone have any advise about this?
I cannot know even what search keyword would be OK for this situation.
(maybe an environment-specific problem? How do you think?)

irb(main):010:0> a = [3,2,1]
=> [3, 2, 1]
irb(main):011:0> a.each
=> #<Enumerable::Enumerator:0x7f413a20d668>
irb(main):012:0> a.each{|x| print x}
321=> [3, 2, 1]
irb(main):013:0> a.each do |x| print x end
321=> [3, 2, 1]
irb(main):014:0> 1.to 9
NoMethodError: undefined method `to' for 1:Fixnum
    from (irb):14
    from :0
irb(main):015:0> 1.to(9) do |x| print x done
irb(main):016:1> 1.to(9) { |x| print x }
irb(main):017:1> 1.to(9)
irb(main):018:1> 1.upto(9)
irb(main):019:1> 1.upto(9) do |x| print x done
irb(main):020:2> 1.upto(9) { |x| print x }
irb(main):021:2> print "x"
irb(main):022:2> abc
irb(main):023:2> a
irb(main):024:2> b
like image 479
plhn Avatar asked Feb 18 '23 17:02

plhn


2 Answers

What happened is that after the error, you typed done instead of end.

Nothing was executed until the block was parsed, but the end never came, so irb just kept reading stuff...

In the future just type ^C or ^D until you get back to the top level or the shell, and then start over.

like image 121
DigitalRoss Avatar answered Feb 27 '23 23:02

DigitalRoss


IRB is waiting for something to close (in this case the do block on line 15 needs an end).

You can notice this by watching the number after the line number (:0, :1, :2...): while it's positive, IRB wants you to close something.

You can press Ctrl+C to abort the current command and start a new one.

like image 44
Michaël Witrant Avatar answered Feb 28 '23 01:02

Michaël Witrant