Ruby

From Ever changing code
Revision as of 10:04, 1 March 2019 by Pio2pio (talk | contribs) (→‎Basic file structure)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Ruby adventures...

Install

Using package manager

Install Ruby using OS package manager it's rarely making usable for projects.

sudo apt-get install ruby ruby-dev

RVM Ruby Version Manager

# Install RVM - Ruby Version Manager
sudo apt-get install -y curl gnupg2 build-essential
gpg2 --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB

# Install RVM stable with ruby:
curl -sSL https://get.rvm.io | bash -s stable --ruby # can take ~10 min as it's compiling sources


To start using RVM you need to run source /home/vagrant/.rvm/scripts/rvm in all your open shell windows, in rare cases you need to reopen all shell windows.

source /home/vagrant/.rvm/scripts/rvm #this in U18 adds following to your env
export PATH=$PATH:/home/vagrant/.rvm/bin #adds local rvm bin to your user path
rvm_user_install_flag=1
rvm_stored_umask=0002
rvm_loaded_flag=1

# Optional: sudo usermod -a -G rvm `whoami`


Use RVM to install Ruby version you want ruby-X.X.X

  • this will search your package management for version you ask if not found
  • will download sources and get it compiled
  • will remove undesired packages, rg. U18 libssl-dev to install libssl1.0-dev instead
rvm install ruby-2.1.0
rvm --default use ruby-X.X.X
  1. Install Bundler

gem install bundler --no-rdoc --no-ri </source>


Install dependencies, uses project's Gemfile

bundler install        #installs gems and their dependencies
bundler install --path #installs gems in ./path dir, much easier to remove them if something is wrong
                       #creates .bundle/config file that contains path to gems


Control your gem bundles and other ussefulness

bundle info kwalify  #display where a gem got installed
  * kwalify (0.7.2)
        Summary: a parser, schema validator, and data-binding tool for YAML and JSON.
        Homepage: http://www.kuwata-lab.com/kwalify/
        Path: /home/vagrant/.rvm/gems/ruby-2.6.0/gems/kwalify-0.7.2

ruby -S gem list --local  #list all gems

Gemfile and Gemfile.lock

The Gemfile is where you specify which gems you want to use, and lets you specify which versions.

The Gemfile.lock file is where Bundler records the exact versions that were installed. This way, when the same library/project is loaded on another machine, running bundle install will look at the Gemfile.lock and install the exact same versions, rather than just using the Gemfile and installing the most recent versions. (Running different versions on different machines could lead to broken tests, etc.) You shouldn't ever have to directly edit the lock file.

Basic file structure

#!/usr/bin/env ruby

# require 'yaml'
require 'fileutils'

file_name="file.txt"

# #{file_name} -: string interpolation
FileUtils.touch("#{file_name}.OK")

The require dependencies need to be installed using

sudo apt  install ruby-bundler
bundle install       #default, but NEVER RUN AS SUDO
bundle install -path #installs gems into ~/.path/