Created
April 8, 2022 08:17
-
-
Save Bahanix/0e4d9340fc607b3687d7b87f8839951f to your computer and use it in GitHub Desktop.
Ruby syntax 101
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xcode-select --install | |
brew update | |
brew install git | |
git clone https://github.com/sstephenson/rbenv.git ~/.rbenv | |
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile | |
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile | |
git clone https://github.com/sstephenson/rbenv-gem-rehash.git ~/.rbenv/plugins/rbenv-gem-rehash | |
git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build | |
source ~/.bash_profile | |
rbenv install 2.6.8 | |
rbenv global 2.6.8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Everything after the '#' character is a comment | |
# Run these lines in an interactive ruby interpreter (irb) to "feel" them | |
9/2 == 4 | |
9/2.0 == 4.5 # Same as 9.0/2 or 9.0/2.0 or 9.to_f/2 | |
2**8 == 256 | |
[1, 2] + [2, 3] == [1, 2, 2, 3] | |
[1, 2] - [2, 3] == [1] | |
[1, 2] | [2, 3] == [1, 2, 3] | |
[1, 2] & [2, 3] == [2] | |
a = [] | |
a << "toto" | |
a == ["toto"] | |
10.times do |n| | |
puts n | |
end | |
10.times { |n| puts n } # oneliner syntax | |
1.upto(10) do |n| | |
puts n | |
end | |
[1, 2, 3].each do |i| | |
puts i | |
end | |
(1..10).each do |n| | |
puts n | |
end | |
condition1 = "" | |
condition2 = false | |
if condition1 | |
puts "condition1 is not false nor nil" | |
elsif condition2 | |
puts "condition1 is false or nil, and condition2 isn't" | |
else | |
puts "condition1 and condition2 are both false or nil" | |
end | |
# Only strongly-typed 'false' and 'nil' (ruby null equivalent) will be rejected by 'if' | |
# Eg. 0 et "" are equivalent to true | |
{ first_name: "Julien" } == { :first_name => "Julien" } | |
{ first_name: "Julien" } != { "first_name" => "Julien" } | |
# :first_name is a symbole. It's a kind of constant string used as a software internal key. | |
my_variable = :first_name | |
my_hash = { first_name: "Julien" } | |
# These all return "Julien" | |
my_hash[my_variable] | |
my_hash[:first_name] | |
{ first_name: "Julien" }[:first_name] | |
my_hash.each{ |key, value| puts value } | |
def my_function | |
42 | |
end | |
my_function == 42 | |
# return isn't a mandatory keyword in a function. Then last line of a function will be returned. | |
def my_function(i) | |
i * 2 | |
end | |
my_function(10) == 20 | |
# These all return the same | |
[1, 2, 3].map{ |i| i * 2 } | |
["1", "2", "3"].map(&:to_i).map{ |i| i * 2 } | |
[2, 4, 6] | |
def my_function(i: 7) | |
i * 2 | |
end | |
my_function(i: 10) == 20 | |
my_function == my_function() # 14 | |
def my_function(i:) | |
i * 2 | |
end | |
my_function(i: 10) == 20 | |
my_function # ArgumentError (missing keyword: i) | |
class User | |
def initialize(name) | |
@name = name | |
end | |
def greet | |
puts "Bonjour #{@name}" | |
end | |
end | |
user = User.new("Julien") | |
user.greet == "Bonjour Julien" | |
user.name # NoMethodError (undefined method `name' for User instance) | |
class User | |
def initialize(name) | |
@name = name | |
end | |
def name | |
@name | |
end | |
def name=(value) | |
@name = value | |
end | |
end | |
user = User.new("Julien") | |
user.name == "Julien" | |
user.name = "Another name" | |
user.name == "Another name" | |
class User | |
attr_reader :name | |
def initialize(name) | |
@name = name | |
end | |
end | |
user = User.new("Julien") | |
user.name == "Julien" | |
user.name = "Another name" # NoMethodError (undefined method `name=' for User instance) | |
class User | |
attr_accessor :name | |
def initialize(name) | |
@name = name | |
end | |
end | |
# @ are instance variables. | |
# Note: in Rails, @variables in controllers have a special meaning |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sudo apt-get update | |
sudo apt-get install build-essential libffi-dev libssl-dev libreadline-dev zlib1g-dev git | |
git clone https://github.com/sstephenson/rbenv.git ~/.rbenv | |
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc | |
echo 'eval "$(rbenv init -)"' >> ~/.bashrc | |
git clone https://github.com/sstephenson/rbenv-gem-rehash.git ~/.rbenv/plugins/rbenv-gem-rehash | |
git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build | |
source ~/.bashrc | |
rbenv install 2.6.8 | |
rbenv global 2.6.8 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment