A useful guide to start with is found here.
-
Install rbenv
-
Check the available versions of Ruby:
rbenv install -l
-
Install the required version:
rbenv install <version>
-
Set the Ruby to the required version:
rbenv global <version> # set the default Ruby version for this machine # or: rbenv local <version> # set the Ruby version for this directory
The first option is to install rbenv for Windows, which only works with PowerShell.
The setup is the same as rbenv installation for UNIX-like OSs.
-
install RubyInstaller as a non-admin installation. Also make sure Ruby is added to the PATH.
-
Install MSYS2 and MinGW when asked.
-
Open the command line and run:
ridk enable
-
Add
.vscode/extensions.jsonwith the following contents:{ "recommendations": [ "ghbozz.hashtag", "formulahendry.code-runner", "kaiwood.endwise", "esbenp.prettier-vscode", "Shopify.ruby-lsp", "sorbet.sorbet-vscode-extension", "KoichiSasada.vscode-rdbg", "ms-vscode.test-adapter-converter", "hbenl.vscode-test-explorer" ] } -
Add
.vscode/settings.jsonwith the following contents:"[ruby]": { "editor.defaultFormatter": "esbenp.prettier-vscode" },
-
Create the root directory.
-
In the root directory, run:
gem install bundler bundle gem . -
On RubyGems.org, search for
rspecand copy the GEMFILE script and paste it to the end of localGemfile. -
Do similar thing for packages REPL:
irb,pry, debug:debug, and documentation:rdoc. -
Do similar thing for optional packages:
test-unit,rubocop,rbs, andrake. -
Install all gems:
bundler install
-
The only boilerplate code you may need is
version.rbthat contains the version of the gem or in this case the project. Put the file inlib/version.rb.# frozen_string_literal: true module Ruby module MyGem VERSION = "0.1.0" end end
-
Update the
project_name.gemspecfile:require_relative 'lib/version' #... spec.version = Ruby::MyGem::VERSION #...
-
Create the library (module) file in
libfolder with the following structure:# my_gem.rb module MyGem def self.greet(name = 'Ruby') "Hello, #{name}!" end end
or
# my_gem.rb class MyGem def greet(name = 'Ruby') "Hello, #{name}!" end end
-
Create the main function in
binfolder:# main.rb require "my_gem" def main(param) param.each do |name| puts MyGem.greet(name) end end main(ARGV)
-
To run the main function:
ruby -I lib bin/main.rb [arguments list]
-
To package the library:
gem build project_name.gemspec
-
Add the unit test file to
spec/my_gem_spec.rbfolder:# my_gem_spec.rb require "my_gem" RSpec.describe MyGem do it "greets with default name" do expect(MyGem.greet).to eq "Hello, Ruby!" end it "greets a custom name" do expect(MyGem.greet("World")).to eq "Hello, World!" end end
-
To run a specific test:
rspec spec/my_gem_spec.rb
-
To run all tests:
rspec
-
Add the unit test file to
tests/ruby_test.rbfolder:# ruby_test.rb require "test/unit" require "my_gem" class Test_Gem < Test::Unit::TestCase def test_1 assert_equal("Hello, ME!", MyGem.greet("ME")) end def test_2 refute_nil(MyGem.greet("ME"), "object shouldn't be nil") end end
-
To run the test:
ruby -I lib tests/ruby_test.rb [-n test_method_to_run]
-
To generate the rbs file, run:
rbs prototype rb --out-dir=sig lib
The prototype files will be created in
sigfolder and often needs modifying and adjusting the types, etc. -
To update the type signature files while skipping the already existing ones, run the command again.
-
To write documentation for RDoc, place a comment above the class, module, method, constant, or attribute you want documented.
-
You can also use POD style documentation for block comments using:
=begin rdoc documents =end
-
To generate HTML document from the folder:
rdoc lib
Example using markdown
# :markup: markdown
#--
# The comments here are ignored.
# my_gem.rb
# The comments after the next line are processed.
#++
# This class uses **Markdown** for documentation.
module MyGem
=begin rdoc
Documentation for **greet** function.
_name_ is the input argument.
=end
def self.greet(name = 'Ruby')
"Hello, #{name}!"
end
endExample using RDoc
#--
# The comments here are ignored.
# my_gem.rb
# The comments after the next line are processed.
#++
# This class uses <b>Markdown</b> for documentation.
module MyGem
=begin rdoc
Documentation for <b>greet</b> function.
<i>name</i> is the input argument.
=end
def self.greet(name = 'Ruby')
"Hello, #{name}!"
end
end