# Strange difference between irb and ruby

**URL:** <https://www.ruby-forum.com/t/strange-difference-between-irb-and-ruby/100607>\
**Category:** Ruby\
**Created:** [July 30, 2006, 9:06pm UTC](https://www.ruby-forum.com/t/strange-difference-between-irb-and-ruby/100607 "2006-07-30T21:06:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![naPOLeon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/58956e/32.png) [@naPOLeon](https://www.ruby-forum.com/u/naPOLeon)\
**Post date:** [July 30, 2006, 9:06pm UTC](https://www.ruby-forum.com/t/strange-difference-between-irb-and-ruby/100607/1 "2006-07-30T21:06:55Z")

</div>

Hello,  
I’m a just beginner to ruby and impressed by the possibility to test  
code in irb. Today I wrote a line of code in irb and it behaved as I  
expected. But when I inserted it in my program and executed with ruby,  
the code-block didn’t worke. Maybe you could tell me why.

The line

a.scan(reg\_exp).each {|w| print "#{w.to\_s}: "; a.sub!(reg\_exp,  
gets.chomp)}

It asked me in irb depending on the size of ‘a’ for some input and  
replaced a part (I used the Regular Expression /\<\w\*\>/) of the string  
‘a’ with my input.  
But if I use this line in a program, the part ‘gets.chomp’ seems to be  
ignored. The result is no change on String ‘a’.

I’ve no explanation for this strange behaviour, I hope you can give me  
a hint. Maybe the whole line is rubbish and there is a lot better way  
to do the job?

Thanks,  
naPOLeon

---

<div class="post-metadata">

**Author:** ![naPOLeon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/58956e/32.png) [@naPOLeon](https://www.ruby-forum.com/u/naPOLeon)\
**Post date:** [July 31, 2006, 12:07am UTC](https://www.ruby-forum.com/t/strange-difference-between-irb-and-ruby/100607/2 "2006-07-31T00:07:24Z")

</div>

naPOLeon wrote:

> Hello,  
> I’m a just beginner to ruby and impressed by the possibility to test  
> code in irb. Today I wrote a line of code in irb and it behaved as I  
> expected. But when I inserted it in my program and executed with ruby,  
> the code-block didn’t worke. Maybe you could tell me why.
> 
> The line
> 
> a.scan(reg\_exp).each {|w| print "#{w.to\_s}: "

w is already a string.

> ```
> ; a.sub!(reg_exp,
> 
> ```
> 
> gets.chomp)}

To me, it doesn’t make sense to change the string  
while you are scanning it.

Try this:

a = "foo bar "  
reg\_exp = /\<\w\*\>/  
a.gsub!(reg\_exp) { |w| print "#{w}: "; gets.chomp }

p a

---

<div class="post-metadata">

**Author:** ![naPOLeon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/58956e/32.png) [@naPOLeon](https://www.ruby-forum.com/u/naPOLeon)\
**Post date:** [July 31, 2006, 12:13am UTC](https://www.ruby-forum.com/t/strange-difference-between-irb-and-ruby/100607/3 "2006-07-31T00:13:45Z")

</div>

On Sunday 30 July 2006 21:05, naPOLeon wrote:

> gets.chomp)}
> 
> It asked me in irb depending on the size of ‘a’ for some input and  
> replaced a part (I used the Regular Expression /\<\w\*\>/) of the  
> string ‘a’ with my input.  
> But if I use this line in a program, the part ‘gets.chomp’ seems to  
> be ignored. The result is no change on String ‘a’.

I don’t know if this is the problem here, but:  
“gets” might not do exactly what you think it does…  
Try changing it to “$stdin.gets”.

Read:  
$ ri Kernel#gets  
That’s what you use now…  
and:  
$ ri IO#gets  
That’s what you call with $stdin.gets.

HTH,  
Stefan

---

<div class="post-metadata">

**Author:** ![naPOLeon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/58956e/32.png) [@naPOLeon](https://www.ruby-forum.com/u/naPOLeon)\
**Post date:** [July 31, 2006, 3:42pm UTC](https://www.ruby-forum.com/t/strange-difference-between-irb-and-ruby/100607/4 "2006-07-31T15:42:26Z")

</div>

Thanks William and Stefan,

your right, gsub! is a better way to do what I want. But your solution  
doesn’t work, either. I think ruby has a problem by invoking gets.chomp  
(by the way Stefan, it made for me no difference to use $stdin.gets  
instead of gets )-: in a block. I hope you know a workaround (I tried  
to write …do…end instead of {}, but it doesn’t changed anything in  
the behaviour of my program)

thanks,  
naPOLeon
