This file contains hidden or 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
module FlippedTable | |
def table | |
"┻━┻" | |
end | |
end | |
class AngryTableFlipper | |
# Including FlippedTable's methods... | |
# as instance methods | |
include FlippedTable |
This file contains hidden or 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
# Let's define a FlippedTable class | |
module FlippedTable | |
# Define method we want to use in different classes | |
def table | |
"┻━┻" | |
end | |
end | |
class AngryTableFlipper | |
# Include it to a class that will use it |
This file contains hidden or 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
class Wheel | |
def tire | |
puts "Tire is flat, needs service" | |
end | |
end | |
Wheel.new.tire | |
# Tire is flat, needs service | |
module SpareTire |
This file contains hidden or 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
# Bang method behavior: upcase! | |
email = "[email protected]" | |
# Upcasing a string, but not changing the variable email's value | |
email.upcase | |
# => "[email protected]" | |
# => "[email protected]" | |
# Using bang to upcase a string |
This file contains hidden or 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
# Using OpenStruct to mimic an object with the 'posts method' | |
user = OpenStruct.new(posts: [:post1, :post2]) | |
# Using if user as a nil checker | |
user.posts if user | |
# => [:post1, :post2] | |
# Can be replaced with the safe navigation operator | |
user&.posts | |
# => [:post1, :post2] |
This file contains hidden or 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
# Ruby 3.1+ | |
users = [ | |
{ name: "Yukihiro Matsumoto", age: 57 }, | |
{ name: "Kabosu the Shiba Inu", age: 16 }, | |
{ name: "Thiago Massa", age: 33 } | |
] | |
def fetch_age_from_person(person, hash) | |
hash => [*, {name: ^person, age: age}, *] |
This file contains hidden or 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
class PlaySong | |
def initialize(song) | |
@song = song | |
end | |
def call | |
# Here you implement your service object | |
"🎶 #{song}" | |
end |
This file contains hidden or 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
# Creating a complex hash, with nested keys | |
my_complex_hash = { | |
users: [ | |
{name: "Yukihiro Matsumoto", age: 57}, | |
{name: "Kabosu the Shiba Inu", age: 16}, | |
{name: "Thiago Massa", age: 33} | |
] | |
} |
This file contains hidden or 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
# Ruby pattern matching based on Type (=> operator) | |
[1,2,3] => [1, Integer, my_variable] | |
# This means: | |
# We want to assign `my_variable` (variable binding), | |
# but only if the first position is 1 and second position is an Integer) | |
my_variable # => 3 ✨ | |
# What if it doesn't match? You get an exception 💣 | |
[1,2,3] => [Integer, String, my_variable] | |
# [1, 2, 3]: String === 2 does not return true (NoMatchingPatternError) |
This file contains hidden or 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
web: rdbg -n --open=vscode -c -- bin/rails server -p 3000 | |
js: yarn build --watch |
NewerOlder