Why did I get "main.using", only allowed when populating, when I used the refinement in the IRB?

I tried using Refinement in IRB (v0.9.6, Ruby 2.3.0):

module Foo refine Object do def foo() "foo" end end end using Foo # => RuntimeError: main.using is permitted only at toplevel 

This is basically a fine tuning from the documentation (which leads to the same error).

Something went wrong? How to fix it?

+5
source share
1 answer

This is either an error or an error in IRb. It is well known that due to the rather hacky way IRb is implemented, it does not behave correctly for all angular cases.

It is impossible, perhaps, everyone knows that in Ruby, methods defined at the top level become private methods of an Object instance, while in IRb they become public methods of an Object instance. Another obvious difference in behavior is that require_relative does not work in IRb because it searches in relation to the current file, but IRb does not have the current file.

There are also some differences in what syntax is accepted, I believe, and something to do with local variables and when they are precisely defined and not defined.

Thus, it is possible that there may be some behavioral differences. Refinements. In fact, I myself came across this error message and ran the same code outside IRb, either using ruby -e , or from a file, or from another REPL, always left.

+5
source

Source: https://habr.com/ru/post/1239928/


All Articles