ruby - Why did I get `main.using is permitted only at toplevel` when I used a Refinement in IRB? -


i tried use refinement in irb (v0.9.6, ruby 2.3.0):

module foo   refine object     def foo() "foo" end   end end  using foo # => runtimeerror: main.using permitted @ toplevel 

this the exact setup documentation (which results in same error).

what went wrong? how fix this?

it's either bug or misfeature of irb. well-known due pretty hackish way irb implemented, not behave correctly corner-cases.

the incompatibility knows in ruby, methods defined @ top-level become private instance methods of object, whereas in irb, become public instance methods of object. obvious behavioral difference in irb, require_relative doesn't work, because searches relative current file, in irb, there is no current file.

there differences in syntax gets accepted, believe, , local variables , when , aren't defined.

so, not inconceivable there might behavioral differences wrt. refinements. in fact, myself have encountered error message, , running exact same code outside irb, either ruby -e, file, or different repl, made go away.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -