-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
567340c
commit 17075f8
Showing
7 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Software design | ||
|
||
Welcome to software design page. Please open menu and enjoy. | ||
|
||
- [See the main script]() | ||
- [Single responsibility for object]() | ||
- [System should do one thing and to it well]() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# How to declare unary operator | ||
|
||
In Ruby a class can have methods like `+`, `-`, etc. | ||
|
||
```ruby | ||
class MyString | ||
def initialize(str) | ||
@str = str | ||
end | ||
|
||
def +(string) | ||
self.class.new("#{@str} #{string}") | ||
end | ||
|
||
def inspect | ||
to_s | ||
end | ||
|
||
def to_s | ||
"#{@str}" | ||
end | ||
end | ||
|
||
a = MyString.new('My String.') | ||
|
||
puts a + 'Add some string' | ||
# "My String. Add some string" | ||
``` | ||
|
||
We also can declare an unary operator like `!object`, `+object`, `-object`, etc. | ||
|
||
```ruby | ||
class MyString | ||
def +@ | ||
# add a space before | ||
self.class.new(" #{@str}") | ||
end | ||
|
||
def -@ | ||
# delete first character | ||
self.class.new(@str[1..-1]) | ||
end | ||
end | ||
|
||
puts +++a + ' Wow!' | ||
# " My String. Wow!" | ||
puts ---a | ||
# "String." | ||
``` | ||
|
||
Some declare unary operators can be declared without `@` | ||
|
||
```ruby | ||
class MyString | ||
def ! | ||
# check string length | ||
@str.size > 0 | ||
end | ||
end | ||
|
||
puts !MyString.new('My String.') | ||
# true | ||
puts !MyString.new('') | ||
# false | ||
``` | ||
|
||
List of methods: | ||
|
||
- `**` | ||
- `!` `~` `+` `+@` `–` `-@` | ||
- `*` `/` `%` | ||
- `>>` `<<` | ||
- `&` | ||
- `^` `|` | ||
- `<=` `<>` `>=` | ||
- `<=>` `==` `===!` `=` `=` `~!` `~` |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# find vs find_by | ||
|
||
`find` - it raises `ActiveRecord::RecordNotFound`` exception | ||
|
||
`find_by` - it can return `nil` | ||
|
||
`find_by!` - it raises `ActiveRecord::RecordNotFound`` exception |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# preload vs includes | ||
|
||
### Joins | ||
|
||
```ruby | ||
User.joins(:posts).where(posts: { active: true }) | ||
``` | ||
|
||
* uses inner join | ||
* does not load associated records in memory. | ||
* executes single query | ||
* it does not solve n+1 problem | ||
|
||
### Preload | ||
|
||
```ruby | ||
User.preload(:posts) | ||
``` | ||
|
||
* does not perform join | ||
* load associated data in memory. | ||
* executes multiple queries | ||
* it solves n+1 problem | ||
|
||
### Includes | ||
|
||
```ruby | ||
User.includes(:posts) | ||
|
||
User.includes(:posts).references(:posts) | ||
``` | ||
|
||
* perform left outer join and store associated data in the memory. | ||
* Sometimes generate one query and sometimes generates 2 queries | ||
* If you want to force to use a single query you can add references | ||
* it solves n+1 problem | ||
|
||
### Eager load | ||
|
||
```ruby | ||
User.eager_load(:posts).where("posts.visible = ?", true) | ||
``` | ||
|
||
* Load associated data in memory | ||
* Performs LEFT OUTER join | ||
* Forces to use a single query | ||
* Some times takes more time than two queries | ||
* it solves n+1 problem | ||
|
||
[source](https://techrello.com/ruby-on-rails/joins-vs-preload-vs-includes-vs-eager-load-in-rails) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Terminal | ||
|
||
Welcome to terminal page. Please open menu and enjoy. |