Installation · Usage · Examples · Contributing · Sponsors
cd "/log" do
ls.each_line do |line|
puts cat(line)
end
endYes, that’s valid Ruby!
ls and cat are just shell commands, but RubyShell makes them behave like Ruby methods.
Install the gem and add to the application's Gemfile by executing:
$ bundle add rubyshell
If bundler is not being used to manage dependencies, install the gem by executing:
$ gem install rubyshell
With RubyShell, every shell command can be used inside the ruby, you just need to call
sh do
puts pwd # => /Users/albertalef/projects/rubyshell
endHere we have different ways to pass arguments to a command. You can separate strings, use only one, use hashes, anyway will work
sh do
docker("ps", all: true) # Using hash syntax = docker ps --all
docker("ps", a: true) # Using hash syntax = docker ps -a
docker("ps", '-a') # Passing multiple strings = docker ps -a
docker("ps -a") # Passing one string = docker ps -a
endHas two possible ways, changing the folder of the code, or running code only inside a folder
sh do
puts pwd # => /Users/albertalef/projects/rubyshell
cd 'examples'
puts pwd # => /Users/albertalef/projects/rubyshell/examples
endsh do
cd 'examples' do
puts pwd # => /Users/albertalef/projects/rubyshell/examples
end
puts pwd # => /Users/albertalef/projects/rubyshell
endThe chain method make possible we use shell operators inside the ruby, like & && | > >> < <<
sh do
chain { echo "Dummy text" >> "dummy.txt" }
puts cat("dummy.txt") # => "Dummy text"
end
sh do
number_of_files = chain { ls | wc('-l') }.chomp
puts number_of_files # => 5
end#!/usr/bin/env ruby
require "rubyshell"
require "securerandom"
sh do
mkdir "files"
cd "files" do
5.times do |i|
chain do
echo(SecureRandom.alphanumeric(16)) >> "#{i}.txt"
end
end
puts "Number of Files: #{ls.lines.count}"
ls.each_line do |filename|
puts cat(filename)
end
end
ensure
rm "-rf files"
end
# Running:
#
# ❯ ./examples/example1.rb
#
# Number of Files: 5
# o6Kw8KHvWJnLGSeQ
# qkRKcZHqu2Moq1se
# nUPluln9GM1ydtoz
# rkdYsc1RBhkeN1dq
# ZPXZMqzYfyFfjPHF- Support to Streams
After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/albertalef/rubyshell. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the Rubysh project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
