SciRuby / iruby

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

Can't add a method to String class in Jupyter-lab #333

Closed nmingotti closed 8 months ago

nmingotti commented 8 months ago

Hi, as title says, in Jupyter-lab , here is an example how what does not work

class String 
    def fs()
        return "foobar"
    end
end 
#
"123".fs

I get "undefined method `fs' for "123":String". Maybe I am missing something silly here but this is very useful to have. bye

kojix2 commented 8 months ago

Hi @nmingotti

The reason for this is that we are in Object, not main.

class ::String 
    def fs()
        return "foobar"
    end
end 
# fs()
"123".fs.

will returns the expected result.

See https://github.com/SciRuby/iruby/issues/326

I am not sure why iruby does this.

nmingotti commented 8 months ago

Thank you, this is an important productivity feature to have for me. I close the issue, but it would be great if somebody could explain why it is this way.