Ruby (Application)
Jump to navigation
Jump to search
Ruby
Install
Linux
# shared/opt install schema v1.5.6
#### as common user ####
# define applications vars
export SOFTWARE_PATH="/home/shared/opt/software"
export NAME="ruby"
export VERSION="2.7.2"
export URL="https://cache.ruby-lang.org/pub/ruby/2.7/ruby-${VERSION}.tar.gz"
su - -w SOFTWARE_PATH,NAME,VERSION
#### as root ####
# install packages and prepare destination path
apt-get autoremove --purge ruby\*
apt-get -q -y install wget coreutils findutils < /dev/null
apt-get -q -y install libreadline-dev libssl-dev zlib1g-dev libgdbm-dev libqdbm-dev < /dev/null
mkdir -m 777 "${SOFTWARE_PATH}/tmp_install/" "${SOFTWARE_PATH}/${NAME}_${VERSION}/"
exit
#### as common user ####
umask 0027
cd "${SOFTWARE_PATH}/tmp_install"
wget -c --no-check-certificate "${URL}"
tar -xzf "ruby-${VERSION}.tar.gz"
cd "ruby-${VERSION}"
# define compiler flags optimizations (from debian dpkg-buildflags command)
export CFLAGS="-g0 -O2 -fstack-protector-strong -Wformat -Werror=format-security -mtune=native -pipe"
export LDFLAGS="-s -Wl,-z,relro"
# configure, build and install
./configure --prefix="${SOFTWARE_PATH}/${NAME}_${VERSION}"
make -s
make install
cd
su - -w SOFTWARE_PATH,NAME,VERSION
#### as root ####
# ensure permissions to destination path
cd "${SOFTWARE_PATH}"
chown -R root:users "${NAME}_${VERSION}"
find "${NAME}_${VERSION}" -type d -exec chmod a-s,u+rwx,g+rx,g-w,o-rwx {} \;
find "${NAME}_${VERSION}" -type f -exec chmod a-s,u+rw,g+r,g-w,o-rwx {} \;
rm -rf tmp_install
ln -s -f -T "${NAME}_${VERSION}" "${NAME}"
exit
#### as common user ####
# test the application (you can put the follow in ~/.profile)
export SOFTWARE_PATH="/home/shared/opt/software"
GEM_HOME="${HOME}/.gem"
GEM_PATH="${GEM_PATH}"
PATH="${PATH}:${SOFTWARE_PATH}/ruby/bin:${GEM_HOME}/bin"
ruby --version
MacOs
Xcode
- note: reset xcode
sudo rm -rf /Library/Developer/CommandLineTools/ xcode-select --install
- note: you may need to setup the follow in your .profile
# from https://github.com/castwide/vscode-solargraph/issues/78#issuecomment-591008159 export SDKROOT=$(xcrun --show-sdk-path)
Homebrew
Ruby from RBenv
- from http://rbenv.org/
brew install rbenv brew link --overwrite rbenv rbenv install 2.6.3 rbenv global 2.6.3
configure ~/.profile
HOME_PATH="${HOME}/bin"
# homebrew
BREW_PATH="/usr/local/sbin:/usr/local/bin"
# ruby
export GEM_HOME="${HOME}/.gem"
export GEM_PATH="${GEM_HOME}"
GEM_HOME_PATH="${GEM_HOME}/bin"
export DISABLE_SPRING=1
# rbenv
OLDPATH=${PATH}
eval "$(rbenv init -)"
export PATH=${OLDPATH}
unset OLDPATH
export RBENV_SHELL=bash
RBENV_PATH="${HOME}/.rbenv/shims:${PATH}"
# ...
export PATH="${HOME_PATH}:${BREW_PATH}:${RBENV_PATH}:${GEM_HOME_PATH}:${PATH}:${NODE_PATH}"
enforce bundler 'recent' version
rm -f ~/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/specifications/default/bundler-1.17.2.gemspec gem install bundler --default gem list | grep bundler
Node
- note: node is optional but plugins depend on it
brew install node
Common
Irb
- You can configure irb behavior in the ~/.irbrc file, for instance:
cat > ~/.irbrc << 'EOF' require 'irb/completion' IRB.conf[:ECHO] = false IRB.conf[:AUTO_INDENT] = true IRB.conf[:SAVE_HISTORY] = 100000 EOF
Gems
- You can install global useful tools using gem command, for instance:
gem install bundler gem install rubocop rubocop-performance rubocop-rspec gem install solargraph gem install pry pry-doc gem install ruby-debug-ide debase
Example
Sample script
cat > hello.rb <<EOF
class HelloWorld
def initialize(name)
@name = name.capitalize
end
def say_hi
puts "Hello #{@name}!"
end
end
hello = HelloWorld.new('World')
hello.say_hi
EOF
ruby hello.rb
# Hello World!
Sample project with tests using RSpec
- prepare project structure and deps:
mkdir lib spec cat > Gemfile << 'EOF' # Gemfile source 'https://rubygems.org' gem 'rspec' EOF
- create project sources:
cat > main.rb << 'EOF'
#!/usr/bin/env ruby
require_relative 'lib/hello_world'
hello = HelloWorld.new('World')
puts hello.get_hi
EOF
cat > lib/hello_world.rb << 'EOF'
# This is an helloworld sample class
class HelloWorld
def initialize(name)
@name = name.capitalize
end
def get_hi
"Hello #{@name}!"
end
end
EOF
- create test spec:
cat > spec/hello_world_spec.rb << 'EOF'
require 'hello_world'
describe HelloWorld do
describe '.new' do
it 'contains Me' do
expect(HelloWorld.new('ME').instance_variable_get("@name")).to eql('Me')
end
end
describe '.get_hi with lowercase' do
it 'return capitalized' do
expect(HelloWorld.new('lowercase').get_hi).to match(/Lowercase/)
end
end
describe '.get_hi with UPPERCASE' do
it 'return capitalized' do
expect(HelloWorld.new('UPPERCASE').get_hi).to match(/Uppercase/)
end
end
describe '.get_hi' do
it 'say Hello' do
expect(HelloWorld.new('something').get_hi).to start_with 'Hello'
end
end
describe '.get_hi' do
it 'exclame' do
expect(HelloWorld.new('something').get_hi).to end_with '!'
end
end
enEOF
- install deps and run:
bundle install # ... bundle exec rspec # ... # Finished in 0.00395 seconds (files took 0.04787 seconds to load) # 5 examples, 0 failures bundle exec ruby main.rb # Hello World!