Skip to content

Snippets

Albert Álef edited this page Feb 3, 2026 · 3 revisions

Snippets

Useful code snippets for integrating RubyShell with your environment.

Zsh Plugin

This Zsh plugin allows you to run RubyShell commands directly in your terminal with the rb prefix:

_rb_parser() {
  local ORIGINAL_BUFFER="$BUFFER"

  # Check if the command starts with 'rb '
  if [[ "$BUFFER" == rb\ * ]]; then
    local rb_cmd="${BUFFER#rb }"
    local rest=""

    # Check for shell operators
    if [[ "$rb_cmd" =~ ^(.+?)(\ *[\|><\&].*)$ ]]; then
      rb_cmd="${match[1]}"
      rest="${match[2]}"
    fi

    # Modify buffer to execute Ruby with RubyShell
    BUFFER=" ruby -r rubyshell -e \"extend RubyShell::Executor; puts($rb_cmd)\"$rest"

    # Add original command to history (for recall)
    print -s "$ORIGINAL_BUFFER"
  fi

  zle accept-line
}

zle -N _rb_parser
bindkey '^M' _rb_parser

Usage

After adding the plugin to your .zshrc:

# List files and count lines
rb ls.lines.count

# Pipe to other commands
rb ls.lines.count | cat

# Redirect to file
rb ls.lines.count > count.txt

# Use with grep
rb git("status", porcelain: true).lines.count

How It Works

  1. Intercepts commands starting with rb
  2. Preserves the original command in history
  3. Wraps Ruby code with RubyShell executor
  4. Handles shell operators (pipes, redirects)
  5. Executes via ruby -r rubyshell -e

IRB Configuration

Add to your .irbrc to have RubyShell available in all IRB sessions:

require "rubyshell/tty/irb"

Pry Integration

Add to your .pryrc:

coming soon

Next: Examples

Clone this wiki locally