Today I started learning a new language: Ruby! Coming from a JavaScript world, this article helped me a lot (backed-up here, should it ever vanish from the Internet). In this article, I sum up everything I've learned so far. It's a sort of memo for myself.
Learning resources
Ruby
- https://openclassrooms.com/courses/lancez-vous-dans-la-programmation-avec-ruby (French)
- http://tryruby.org: awesome interactive tutoral
- http://rubykoans.com/: learn by doing TDD (Test-Driven Development): make all tests pass
Rails
- http://rubyonrails.org/doctrine/
- CHOOSING RUBY ON RAILS FOR YOUR NEXT WEB DEVELOPMENT PROJECT
- Code School: Rails for Zombies
- Initiez-vous à Ruby on Rails
- Continuez avec Ruby on Rails
Install
Install rbenv. In short, here is how:
sudo apt update && sudo apt install openssl libicu-dev libssl-dev libreadline-dev zlib1g-dev
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
cd ~/.rbenv && src/configure && make -C src
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.zshrc
~/.rbenv/bin/rbenv init
# Open a new shell
mkdir -p "$(rbenv root)"/plugins
git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build
rbenv install 2.5.0
rbenv global 2.5.0
# Check everything is installed and correctly set up
curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-doctor | bash
Then, add must-have stuff:
gem install bundler
# In a fresh new Ruby project folder, create a Gemfile
rbenv local 2.5.0 # Set version 2.5.0 for this project, same as .nvmrc for Node projects
bundle init
bundle install --path vendor --binstubs bin # Set up .bundle config to install packages to the local folder
# Add vendor and .bundle to your .gitignore file
# In a Ruby project folder, install all dependencies
bundle install # or simply `bundle`
Upgrade Ruby version
Update the version in Gemfile
and .ruby-version
. Then run:
cd $HOME/.rbenv/plugins/ruby-build && git pull && cd -
rbenv install && rbenv rehash
gem install bundler
bundle
rbenv global "$(cat .ruby-version)"
Stuff to know
;
is optional- parentheses optional in
if
statements ==
vs===
: https://stackoverflow.com/questions/4467538/what-does-the-operator-do-in-rubyputs
(adds new line) vsprint
(no new line).each do |item| ... end
10.times do |i| ... end
if
,elsif
,else
,end
array = [1,2]
array[0]
- When you place a colon in front of a simple word, you get a Ruby symbol:
:a
hash = {a:1,b:2} # creates symbols :a and :b
hash[:a]
hash['ok'] = 'yolo' # ok is not a symbol
return
statement optional, last line is implicitely returnedfunc(a,b)
==func a, b
obj.func
==obj.func()
def initialize ... end
@classMemberVariable
def func
==def func()
return true if ....
==if ...<line break>return true<line break>end
- Inheritance:
class Child < Mother
- Replace first occurrence in string:
string['word'] = 'new word'
ratings = Hash.new(0)
: all new hashes will default to 0 as their valuedo..end
vs curly braces for blocks in Ruby: https://stackoverflow.com/questions/5587264/do-end-vs-curly-braces-for-blocks-in-rubypry
: pauses execution but does not give access to local variablesbinding.pry
: pauses execution, gives access to local variables but also pauses all threads (server and tests for instance)whereami
==@
(in binding.pry / pry)help
(get help in binding.pry /pry)PRY_RESCUE_RAIL=1 rails test
: runs a test and pauses on falsy assertions
Ranges
x..y
(y included) vsx...y
(y not included)
Tests
assert_equal x,y
assert_match(/regex/, x)
Resources
- Ruby ampersand colon shortcut
- What does to_proc method mean?
- Closures in Ruby
- What does &. (ampersand dot) mean in Ruby?
- Ruby & (Ampersand) Parameter Demystified
- Ruby 2 Keyword Arguments
- What does a double * (splat) operator do
- What do =~ and /\ mean in Ruby?
- %Q, %q, %W, %w, %x, %r, %s
- Rails Routing from the Outside In
- Multiline strings in Ruby 2.3 - the squiggly heredoc
- Making sense of ActiveRecord joins, includes, preload, and eager_load
- What's the difference between colon “:” and fat arrow “=>”