Ruby Study
最近都在看CSS沒什麼時間繼續看Ruby,今天上codecademy練習ruby。 在這裡紀錄一下之前沒看過的語法。
Multiple comments
1 2 3 4 |
|
Get input from console
1 2 |
|
Different between string.downcase and string.downcase!
With ! the user’s string is modified in-place; otherwise, Ruby will create a copy of user_input and modify that instead
1 2 3 4 5 |
|
Hash sort_by and Array sort!
1 2 3 4 5 6 7 8 |
|
What’s Symbol? it’s not a string
1 2 3 4 5 6 7 8 |
|
Why need symbol? 主要是用來當hash的key以及用來參考到method的名字
- They’re immutable, meaning they can’t be changed once they’re created
- Only one copy,節省記憶體
- Hashes that use symbols instead of strings as keys work faster
來看一下怎麼使用
1 2 3 4 5 6 7 8 9 10 |
|
switch conidition
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Ternary conditional expression
1
|
|
Conditional assignment.
1 2 3 4 5 |
|
upto and downto
1 2 3 4 |
|
Call and Response
Here we use symbol to reference a method name. For example, we want to check variable has next method or not.
1 2 |
|