Created
October 22, 2018 11:00
-
-
Save DavidRails/28cdbf5073dd557d33082a9465f94e5b to your computer and use it in GitHub Desktop.
Ruby practice
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
# 練習:請印出從 1 到 100 之間所有的單數。 | |
array_1_to_100=[*1..100] | |
array_1_to_100_odd=array_1_to_100.select{|a| a%2==1} | |
p array_1_to_100_odd | |
# 練習:請印出從 1 到 100 之間所有的單數的總和。 | |
sum=0 | |
for i in 1..100 do | |
sum=sum+i if i%2==1 | |
end | |
p sum | |
# 練習:改良版土砲 times 方法 | |
#5.my_times { |i| puts i } # 印出數字 0 ~ 4 | |
for i in 0..4 do | |
puts i | |
end | |
# 練習:土砲 select 方法 | |
#[1, 2, 3, 4, 5].my_select { |i| i.odd? } # 只印出單數 1, 3, 5 | |
array_1_to_5_odd=[] | |
for i in 1..5 do | |
array_1_to_5_odd<<i if i%2==1 | |
end | |
p array_1_to_5_odd | |
# 也看了 Scaffold model/view/controller 的 code 有比較之前了解,特別是在class/module和,riethods的部份, | |
# 但是在(1) Rails API | |
# (2) View .erb和 .jbuilder 因為沒有前端的背景,所以不了解 | |
# (3) symbol 的用法似乎會被當作method 像是before_action:set_post,only 〔:show, edit,:update, destroy], | |
# 這部份也不了解,下次上課再請教您,謝謝。 |
kaochenlong
commented
Oct 23, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment