I am using iruby in my jupyter notebook and the kernel is working as expected and accepting all kind of ruby syntax
Unfortunately the %%bash command leads to an error. So no console commands are usable in my notebook. If i switch back to the basic python kernel, the %%bash commands are working as expected.
How can i achieve that the bash commands are working in the iruby kernel? Is there something I forgot to install or set up?
I use following settings in Ubuntu 20.04:
I also use rvm to manage my ruby version.
Basic setup
sudo apt install python3-pip python3-dev
sudo -H pip3 install --upgrade pip
sudo -H pip3 install virtualenv
pip install jupyter
Installing iruby
gem install ffi-rzmq
gem install iruby --pre
iruby register --force
setup and start
rvm use ruby-2.7.2
mkdir ~/ruby_notebook
virtualenv ~/ruby_notebook/ruby_env
source ~/ruby_notebook/ruby_env/bin/activate
jupyter notebook --notebook-dir ~/ruby_notebook
Any ideas how to solve this issue at all so that I can use bash commands?
======== UPDATE and Solution ======
Thanks to engineersmnky I found the solution:
like in python notebook I want to achieve following output:
with the command %x(ls -la)
I get an execution but the output looks different like
So to get a correct output I added following:
%x(ls -la).each_line(chomp: true){|s| p s}
which comes close to the output but followed by an exection strinfg output in the notebook.
So my final solution, also to make the execution command less complicated I am using a method in the beginning of my notebook now.
def execute_console(input)
%x(#{input}).each_line(chomp: true){|s| p s}
return nil
end
This way I can execute special command sin my notebook using rails like
rails routes
in
execute_console('rails routes')
and the output is like expected without any string output.
Syntax errors are printed out as expected as well.
To execute commands at a system level you can use any of the following:
Kernel#system
,%x
(see below for more about percent strings), or Kernel#` ("back ticks")These will all run in process and return the output as a String.
If you want the commands to run in a separate process you can use other options such as
IO.popen
orfork { exec('pwd') }
( if supported by the OS)TL;DR Why the error is occurring
Now to explain your actual error and why it is occurring.
This line is but
%%bash
is being interpreted by ruby and it is looking for a closing%
.%
is another way of creating a string literal (called a "Percent String").The interesting part about this syntax is it does not really have a defined delimiter. (essentially most non-alphanumerics can open and close)
The last line of the docs states
In your case
%
is being used as the delimiter: e.g.but since there is no closing
%
this is being interpreted as:Thus the error you are receiving because the
String
("bash\npwd
) is unterminated.