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
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 "
Regex
Ref:
String operation
- Use String#match:
' '.match /\s{1,}/ # => #<MatchData:0x118ca58> 'f'.match /\s{1,}/ # => nil
- Use Regex#match:
/\s{1,}/.match ' ' # => <MatchData:0x11857e4> /\s{1,}/.match 'f' # => nil
- Use String#=~:
' ' =~ /\s{1,}/ # => 0 'f' =~ /\s{1,}/ # => nil
- Use Regex#=~:
/\s{1,}/ =~ ' ' # => 0 /\s{1,}/ =~ 'f' # => nil
- Use Regex#=== (this is what is used in case statements):
/\s{1,}/ === ' ' # => true /\s{1,}/ === 'f' # => false
Note:
String#===
doesn’t do what you want:' ' === /\s{1,}/ # => false 'f' === /\s{1,}/ # => false