Skip to content

Overwritten Methods

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

Overwritten Methods

Some methods have special behavior in RubyShell to provide better Ruby integration.

cd - Change Directory

Unlike the shell cd, RubyShell's cd supports block syntax:

sh do
  cd "/var/log" do
    # Commands run in /var/log
    puts tail("-n", "5", "syslog")
  end
  # Automatically returns to original directory
end

Without block

Changes directory permanently for the session:

sh do
  cd "/tmp"
  pwd  # /tmp
end

Nested cd blocks

sh do
  cd "/var" do
    cd "log" do
      # Now in /var/log
    end
    # Back to /var
  end
  # Back to original directory
end

ls - List Directory

RubyShell's ls overrides IRB's default ls command to provide actual directory listing:

sh do
  files = ls.lines
  files.each { |f| puts f }
end

This is particularly useful in interactive Ruby sessions (IRB/Pry) where ls would normally show object methods.

clear - Clear Terminal

Clears the terminal screen:

sh do
  clear
end

Source Code

The implementation can be found in lib/rubyshell/overwrited_commands.rb in the repository.


Next: Debugging

Clone this wiki locally