Skip to content

Instantly share code, notes, and snippets.

@mctaylorpants
Last active November 18, 2022 00:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mctaylorpants/04a9353583681f48d90d4ac9f58d3485 to your computer and use it in GitHub Desktop.
Save mctaylorpants/04a9353583681f48d90d4ac9f58d3485 to your computer and use it in GitHub Desktop.
EvalRuby plugin for Neovim
# EvalRuby: Run Ruby without leaving Neovim
#
# Usage:
# - While on a line of Ruby, or in Visual mode
# with multiple lines selected, type :EvalRuby.
#
# Installation:
# 1. Install neovim-ruby: https://github.com/neovim/neovim-ruby
#
# 2. Put this file in your plugins directory
# (Default: ~/.config/nvim/rplugin/ruby)
#
# 3. Run :UpdateRemotePlugins to register the
# new plugin.
#
# 4. Restart Neovim.
#
Neovim.plugin do |plug|
plug.command(:EvalRuby, range: true) do |nvim, range_start, range_end|
ruby_code = nvim
.get_current_buf
.lines[(range_start - 1)..(range_end - 1)]
.join("\n")
result = begin
eval ruby_code
rescue => e
"! #{e.message} (#{e.class})"
end
nvim.get_current_buf.append(range_end, "# => #{result.inspect}")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment