qifan
qifan

Reputation: 7

why Ruby String each_char method prints an extra % at the end

I'm a Ruby newbie, I tried to print each char in a Ruby string, using

"hello world".each_char {|c| print c}

However, when I ran the .rb program, it printed out hello world%, with a % character at the end. Then I switched to irb, it worked fined without the extra % character. Can anyone tell me how this happened? Why there was a %?

Upvotes: 0

Views: 302

Answers (1)

Mircea
Mircea

Reputation: 10566

the program is doing what is expected.
the % is actually the shell prompt. guessing you do something like:

%my-script.rb
hello world%

because you don't have a new line, when the output finishes the script just takes control back and shows the prompt

Upvotes: 0

Related Questions