Skip to content

Commit b078cce

Browse files
committed
Updates README.md
Auto commit by GitBook Editor
1 parent 36b6165 commit b078cce

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ source 在 [Github](https://github.com/Unayung/ruby-on-rails-developer-interview
2828
* [如何用單一 action 套用 /beer/(beer\_type) 這種 route](beer_type.md)
2929
* [如何讓 Person model 可以指定另一個 person 為 parent](person.md)
3030
* [請解釋這個語法 a ||= b](aorequalb.md)
31+
* [請解釋對字串使用 += 和使用 concat 方法有何差異](concat.md)
3132
* [CSRF 是什麼](csrf.md)
3233
* [XSS 是什麼](xss.md)
3334

SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* [如何用單一 action 套用 /beer/(beer\_type) 這種 route](beer_type.md)
1515
* [如何讓 Person model 可以指定另一個 person 為 parent](person.md)
1616
* [請解釋這個語法 a ||= b](aorequalb.md)
17+
* [請解釋對字串使用 += 和使用 concat 方法有何差異](concat.md)
1718
* [CSRF 是什麼](csrf.md)
1819
* [XSS 是什麼](xss.md)
1920

concat.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# 請解釋對字串使用 += 和使用 concat 方法有何差異
2+
3+
對字串使用 += 方法,例如 a += b。事實上是再產生了一個 a 物件並把 a+b 的結果丟進新的 a 物件。我們可以觀察 object_id 來得知這樣的行為。
4+
5+
```ruby
6+
irb(main):041:0> x = "hello"
7+
=> "hello"
8+
irb(main):042:0> x.object_id
9+
=> 70232477009280
10+
irb(main):043:0> x += " world"
11+
=> "hello world"
12+
irb(main):044:0> x
13+
=> "hello world"
14+
irb(main):045:0> x.object_id
15+
=> 70232475548960
16+
```
17+
18+
而 concat 則是對同一個物件進行操作
19+
20+
```ruby
21+
irb(main):049:0> x = "hello"
22+
=> "hello"
23+
irb(main):050:0> x.object_id
24+
=> 70232488144300
25+
irb(main):051:0> x.concat " world"
26+
=> "hello world"
27+
irb(main):052:0> x
28+
=> "hello world"
29+
irb(main):053:0> x.object_id
30+
=> 70232488144300
31+
```

0 commit comments

Comments
 (0)