On 8 June 2014 18:37, Arup Rakshit <aruprakshit / rocketmail.com> wrote:

>
> 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"* ?
>
>
The syntax you're using is PERCENT <SOMETHING> <STRING> <SOMETHING>

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 <SPACE><ENTER> on line 4, it should return the string "i\n"

-- 
  Matthew Kerwin
  http://matthew.kerwin.net.au/