strange difference between irb and ruby

N

naPOLeon

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
 
W

William James

naPOLeon said:
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 <trew> bar <tich>"
reg_exp = /<\w*>/
a.gsub!(reg_exp) { |w| print "#{w}: "; gets.chomp }

p a
 
S

Stefan Lang

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 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
 
N

naPOLeon

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top