Ruby

rack: webserver interface

rbenv

brew install rbenv
echo 'eval "$(rbenv init -)"' >> ~/.zshrc
rbenv install --list
rbenv install 2.4.3
rbenv local 2.4.3 # set ruby for this project
rbenv global 2.4.3 # set ruby for global
source ~/.zshrc

ruby -v
which ruby
which bundle
gem env

If Bundle is not using rbenv ruby

gem install --no-ri --no-rdoc bundler
rbenv rehash

Ruby vendoring

bundle install --path vendor
echo 'vendor/ruby' >> .gitignore

bundle package

If install is missing. solution

git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build

Put gem executable at /bin folder

bundle install --binstubs

Dependencies conflict

1. Remove `Gemfile.lock`
2. Check `Gemfile` fix the dependencies
3. Rerun `bundle install`

Gem development

gem build GEMNAME.gemspec
gem install gemname-version.gem 
gem push gemname-version.gem # update to https://rubygems.org/

# add gem owner
gem owner EMAIL GEMNAME

Parse RSS

require 'rubygems'
require 'rss'
require 'open-uri'

@rss = RSS::Parser.parse('https://medium.com/feed/gojekengineering')

Code Snippet

Change key name

hash[:new_key] = hash.delete :old_key

Compare array of string: WithInDifferentAccess

Class

class Example
  attr_reader :name, :age

  def initialize args
    args.each do |k,v|
      instance_variable_set("@#{k}", v) unless v.nil?
    end
  end
end

Array

arr_of_stirng = ["foo", "bar"]
arr_of_string_2 = %w(foo bar) # shortcut to write array without \"

Tips and trick

# make compile faster for jruby
JRUBY_OPTS=--dev rspec

# alias for bundle exec
alias be="bundle exec "