SciRuby / iruby

Official gem repository: Ruby kernel for Jupyter/IPython Notebook
https://github.com/SciRuby/iruby
MIT License
890 stars 27 forks source link

self no longer works with PlainBackend #303

Closed UliKuch closed 3 years ago

UliKuch commented 3 years ago

Hi! Thanks for maintaining this Gem, I really enjoy using Jupyter notebooks with Ruby 🙂

Unfortunately, when using the PlainBackend in version 0.6 or 0.7 together with Ruby 3.0.1, I am having issues with the self object. It no longer evaluates to main, but to some other object: grafik As I verified by running the line in an irb session, the object seems to be the one created by https://github.com/SciRuby/iruby/blob/fb20cc30c2cb6e104b5398a6a6d907c699743b5a/lib/iruby/backend.rb#L42

Because of this, I can no longer include any modules inside a notebook.

When I remove the dup in the quoted line, everything seemed to work fine, but I am not sure if that breaks anything else.

Or am I missing some error on my part here? I am running my notebooks via the jupyter_on_rails gem, but the issue does not seem to lie there.

kojix2 commented 3 years ago

Because of this, I can no longer include any modules inside a notebook.

Could you please explain more about this point?

UliKuch commented 3 years ago

Could you please explain more about this point?

If, inside a notebook, I try including a module, I receive a NoMethodError:

module Foo
  def bar
    puts "baz"
  end
end

include Foo
# NoMethodError: undefined method `include' for #<Object:0x00007f87fc58d960 @prompt={:PROMPT_I=>"3.0.1 :%03n > ", :PROMPT_S=>"3.0.1 :%03n%l> ", :PROMPT_C=>"3.0.1 :%03n > ", :PROMPT_N=>"3.0.1 :%03n?> ", :RETURN=>" => %s \n", :AUTO_INDENT=>true}>
# (irb):in `<main>'

In version 0.5, this used to work fine. And in any regular irb shell it works, too.

Extending the module instead seems to work though:

extend Foo
# #<Object:0x00007f87fc58d960 @prompt={:PROMPT_I=>"3.0.1 :%03n > ", :PROMPT_S=>"3.0.1 :%03n%l> ", :PROMPT_C=>"3.0.1 :%03n > ", :PROMPT_N=>"3.0.1 :%03n?> ", :RETURN=>" => %s \n", :AUTO_INDENT=>true}>

bar
# baz
kojix2 commented 3 years ago

I see. I understand. I was not aware of this problem.

mrkn commented 3 years ago

@UliKuch Thank you for reporting this issue. This is bug. I'll fix it asap.

mrkn commented 3 years ago

@UliKuch Please try the version 0.7.2.

kylekyle commented 3 years ago

Works great! Thanks!

UliKuch commented 3 years ago

Including a module now works fine, thanks a lot @mrkn !