Ruby, Python code fragment execution of selection in Emacs

I wrote 100 narray exercises which is a Ruby version of 100 numpy exercises in Markdown, but I wrote it in it. You'll need to copy and paste a piece of code. It was quite annoying to repeat this copy and paste execution, so I investigated how to make it possible to execute the selection range of the file opened in Emacs with a single M-x command.

environment

CentOS Linux release 7.2.1511 (Core)
GNU Emacs 24.3.1 (x86_64-redhat-linux-gnu, GTK+ Version 3.8.8)

Sample Markdown

The purpose is to select and execute the code part in this Markdown.

## Sample MD

Python:
```python
import numpy as np
Z = np.arange(10)
print(Z)
```

Ruby:
```ruby
require "numo/narray"
z = Numo::DFloat.new(10).seq
```

Executing a piece of Python code

There are two ways to run Python in an Emacs buffer: python.el and python-mode.el.

(1) python.el that comes with Emacs

It is included in Emacs as standard, so no installation is required.

~ / .emacs.el settings

(require 'python)

This setting is unnecessary because it will be loaded automatically when you open .py, but it is necessary because it will not be loaded when you execute Markdown code pieces.

Run selection

Open the Markdown file in Emacs, select the Python code, and run the following command.

M-x python-shell-send-region

Then, python in interactive mode starts, and the execution result is recorded in the buffer * Python [filename] *, but it does not appear in the foreground, so you need to swithc-to-buffer.

(2) python-mode.el

This also supports IPython.

Installation

Get the source from the python-mode.el site and install it.

~ / .emacs.el settings

(require 'python-mode)

This is also needed if you want to run a piece of Markdown code.

Run selection

M-x py-execute-region

Execution result

The interactive mode python starts, and the buffer * Python * appears on the split screen and the execution result is displayed.

2016-07-31 14-51-20.png

For IPython

If you want to pass it to IPython instead of interactive mode python, use the following command.

M-x py-execute-region-ipython

Ruby code snippet execution

Inf-ruby.el (Inferior Ruby Mode) is used to execute Ruby in the Emacs buffer, but there are also two types.

(1) inf-ruby.el attached to Ruby

What is included in the Ruby source code. The update has stopped for a long time.

Installation

Place misc / inf-ruby.el in the Ruby source tree in an appropriate path.

~ / .emacs.el settings

(autoload 'run-ruby "inf-ruby" "Run an inferior Ruby process")
(require 'inf-ruby)

irb process start

M-x run-ruby

Run selection

M-x ruby-send-region

(2) Derived version of inf-ruby.el

Developed at https://github.com/nonsequitur/inf-ruby. This has been modified from the Ruby attachment version above, and also supports Pry. The version I tried was 2.4.0.

Installation

Get inf-ruby.el from the above URL. When using irb, many useless prompts are displayed as it is and it is difficult to see, so modify it by adding --inf-ruby-mode to the option and then place it in an appropriate path.

diff --git a/inf-ruby.el b/inf-ruby.el
index deb5769..39f586d 100755
--- a/inf-ruby.el
+++ b/inf-ruby.el
@@ -83,7 +83,7 @@ Also see the description of `ielm-prompt-read-only'."
   :group 'inf-ruby)

 (defcustom inf-ruby-implementations
-  '(("ruby"     . "irb --prompt default --noreadline -r irb/completion")
+  '(("ruby"     . "irb --prompt default --inf-ruby-mode -r irb/completion")
     ("jruby"    . "jruby -S irb --prompt default --noreadline -r irb/completion")
     ("rubinius" . "rbx -r irb/completion")
     ("yarv"     . "irb1.9 -r irb/completion")

~ / .emacs.el settings

(autoload 'inf-ruby "inf-ruby" "Run an inferior Ruby process" t)

irb process start

M-x inf-ruby

Run selection

M-x ruby-send-region

Execution result

2016-07-31 14-52-43.png

For Pry

If you want to use Pry instead of irb, add the following line to ~ / .emacs.el.

(setq inf-ruby-default-implementation "pry")

reference: http://d.hatena.ne.jp/rubikitch/20140627/pry

Recommended Posts

Ruby, Python code fragment execution of selection in Emacs
A collection of code often used in personal Python
Difference between Ruby and Python in terms of variables
List of Python code used in big data analysis
Reproduce the execution example of Chapter 4 of Hajipata in Python
Reproduce the execution example of Chapter 5 of Hajipata in Python
Equivalence of objects in Python
2.x, 3.x character code of python
Generate QR code in Python
External command execution in Python
Implementation of quicksort in Python
Character code learned in Python
Measure the execution result of the program in C ++, Java, Python.
Comparison of exponential moving average (EMA) code written in Python
Decrypt one line of code in Python lambda, map, list
Implemented the algorithm of "Algorithm Picture Book" in Python3 (selection sort)
Always check PEP8 while editing Python source code in Emacs
Pixel manipulation of images in Python
Multi-stage selection (Go / C # / Ruby / Python)
[Python] Generate QR code in memory
Automatically format Python code in Vim
Division of timedelta in Python 2.7 series
MySQL-automatic escape of parameters in python
Measure function execution time in Python
Implementation of life game in Python
Waveform display of audio in Python
I wrote the code to write the code of Brainf * ck in python
Write selenium test code in python
Python unittest module execution in vs2017
Law of large numbers in python
Implementation of original sorting in Python
Reversible scrambling of integers in Python
Code tests around time in Python
Python> import this> Show Easter egg> Tried in online code execution environment
What Emacs users should know when writing python code in Sublime Text
Code reading of faker, a library that generates test data in Python
Code reading of Safe, a library for checking password strength in Python
Installation of Visual studio code and installation of python
Conversion of string <-> date (date, datetime) in Python
Fourier series verification code written in Python
Check the behavior of destructor in Python
Differences between Ruby and Python in scope
(Bad) practice of using this in Python
General Theory of Relativity in Python: Introduction
Debug step execution in Python (Bottle, Intellij)
Display a list of alphabets in Python 3
Comparison of Japanese conversion module in Python3
Big difference in ruby, python, httpd performance
Summary of various for statements in Python
[Python3] Rewrite the code object of the function
Automatically generate Python Docstring Comment in Emacs
Referencing INI files in Python or Ruby
The result of installing python in Anaconda
Gang of Four (GoF) Patterns in Python
The basics of running NoxPlayer in Python
Bulk replacement of strings in Python arrays
Project Euler # 16 "Sum of Powers" in Python
Traffic Safety-kun: Recognition of traffic signs in Python
Summary of built-in methods in Python list
Non-logical operator usage of or in python
In search of the fastest FizzBuzz in Python