Skip to content

Commit

Permalink
Add more info
Browse files Browse the repository at this point in the history
  • Loading branch information
kopylovvlad committed Oct 6, 2023
1 parent 567340c commit 17075f8
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 0 deletions.
7 changes: 7 additions & 0 deletions docs/design/README.md
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]()
76 changes: 76 additions & 0 deletions docs/ruby/045.md
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:

- `**`
- `!` `~` `+` `+@` `` `-@`
- `*` `/` `%`
- `>>` `<<`
- `&`
- `^` `|`
- `<=` `<>` `>=`
- `<=>` `==` `===!` `=` `=` `~!` `~`
1 change: 1 addition & 0 deletions docs/ruby/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Welcome to Ruby page. Please open menu and enjoy.
- [String interpolation and to_s method](ruby/042.md)
- [Decorators for methods](ruby/043.md)
- [Private and protected](ruby/044.md)
- [How to declare unary operator](ruby/045.md)

## Links

Expand Down
7 changes: 7 additions & 0 deletions docs/ruby_on_rails/014.md
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
50 changes: 50 additions & 0 deletions docs/ruby_on_rails/015.md
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)
2 changes: 2 additions & 0 deletions docs/ruby_on_rails/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Welcome to Ruby on Rails page. Please open menu and enjoy.
- [How to share data between class and instance](ruby_on_rails/011.md)
- [Security tools](ruby_on_rails/012.md)
- [Railsdiff](ruby_on_rails/013.md)
- [find vs find_by](ruby_on_rails/014.md)
- [preload vs includes](ruby_on_rails/015.md)

## Links

Expand Down
3 changes: 3 additions & 0 deletions docs/terminal/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Terminal

Welcome to terminal page. Please open menu and enjoy.

0 comments on commit 17075f8

Please sign in to comment.