Ruby

From campisano.org
Jump to navigation Jump to search

Ruby

Install

Using ruby binaries

# shared/opt install schema v1.3.1
su -
#### as root ####
# cleanup
apt-get autoremove --purge ruby\*
# dependencies
apt-get -qq -y install wget coreutils findutils
apt-get -qq -y install libreadline-dev libgdbm-dev
# binary
SOFTWARE_PATH="/home/shared/opt/software"
NAME="ruby"
VERSION="2.7.2"
URL="https://cache.ruby-lang.org/pub/ruby/2.7/ruby-${VERSION}.tar.gz"
umask 0027
mkdir -p "${SOFTWARE_PATH}/tmp/${NAME}_${VERSION}"
cd "${SOFTWARE_PATH}/tmp"
wget -c --no-check-certificate "${URL}"
tar -xzf "ruby-${VERSION}.tar.gz"
cd "ruby-${VERSION}"
CFLAGS="-g0 -O3 -march=native -s" ./configure --prefix=${SOFTWARE_PATH}/${NAME}_${VERSION}
make
make install
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
ln -s -f -T "${NAME}_${VERSION}" "${NAME}"
exit
  • and setup your user environment (you can put the follow in ~/.profile)
export SOFTWARE_PATH="/home/shared/opt/software"
export GEM_PATH="${HOME}/.gem"
export GEM_HOME="${GEM_PATH}/gem_home"
export PATH="${PATH}:${SOFTWARE_PATH}/ruby/bin:${GEM_HOME}/bin"

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!