Why in IRB modulo string literal(%) is behaving differently?

2.0.0-p451 :001 > [1,2].map { |i| % i }
=> [“i”, “i”]

within a block it is ok

2.0.0-p451 :002 > % i
2.0.0-p451 :003"> "
2.0.0-p451 :004">

top level it is not working.

But when the same I am writing in my test.rb file, both are working :

[1, 2, 3].map { |i| % i } # => [“i”, “i”, “i”]

% i # => “i”

Why in IRB % i didn’t give “i” ?

Regards,
Arup R.

Debugging is twice as hard as writing the code in the first place.
Therefore,
if you write the code as cleverly as possible, you are, by definition,
not
smart enough to debug it.

–Brian Kernighan

On 8 June 2014 18:37, Arup R. [email protected] wrote:

But when the same I am writing in my test.rb file, both are working :

[1, 2, 3].map { |i| % i } # => [“i”, “i”, “i”]

% i # => “i”

Why in IRB % i didn’t give “i” ?

​The syntax you’re​ using is PERCENT

The somethings can either be matching brackets, or a “safe” character.
For
example:

%(Hello world) # => “Hello world”
%!Hello world! # => “Hello world”

What you’ve done is %_i_ except instead of underscores, you’ve used a
space character. In the block you have a space before and after the i,
so
it works. In the top level sample you have a space before, but not one
after. That’s why IRB has gone into “string reading” mode – notice that
the prompt becomes "> ? That means it’s waiting for the end of the
string.

If you hit on line 4, it should return the string “i\n”

On Sunday, June 08, 2014 09:22:48 PM Matthew K. wrote:

​The syntax you’re​ using is PERCENT

Ahh! That’s I missed. Excellent.

the prompt becomes "> ? That means it’s waiting for the end of the string.

If you hit on line 4, it should return the string “i\n”

Yes. You are correct. I forgot the basic PERCENT(%) rule.

2.0.0-p451 :001 > % i
2.0.0-p451 :002"> ^C
2.0.0-p451 :002 > % i
=> “i”
2.0.0-p451 :003 >

Regards,
Arup R.

Debugging is twice as hard as writing the code in the first place.
Therefore,
if you write the code as cleverly as possible, you are, by definition,
not
smart enough to debug it.

–Brian Kernighan