Ruby on codecademy |
|
INTRODUCTION TO RUBY |
|
1. |
my_num =25 # Add your code here! boolean =true # And here! my_string = "Ruby" # Also here. 直觀的宣告(如Python) |
2. |
+ - * / % ,Exponentiation** |
3. |
puts 相當於print 再斷行 p 用來顯示資料完整型態 |
4. |
"I love espresso".length 字串長度 |
5. |
"ukiM".reverse 從後面開始讀 |
6. |
puts "eric".upcase 轉大寫 puts "eric".downcase 轉小寫 |
7. |
=begin ……… =end /* ………. */ |
8. |
sum = 13+379 product = 923 * 15 quotient = 13209 / 17 |
9. |
name = "ukiM".downcase.reverse.upcase #name.method1.method2.method3 |
CONTROL FLOW IN RUBY |
|
10 |
print "What's your first name?" first_name = gets.chomp first_name.capitalize! last_name = gets.chomp last_name.capitalize! print "What's city are yoy from?" city = gets.chomp city.capitalize print "What status or province are you from?" status = get.chomp status.upcase! puts "Your name is #{first_name} #{last_name} and you're from #{city}, #{status}!" =========================================================== gets.chomp讀入是字串 如果要讀入數字可以用Integer(gets.chomp) |
11. |
hungry = false unless hungry puts "I'm writing Ruby promgrams!" else puts "Time to eat!" end ===========================================================Output:I'm writing Ruby programs! |
12. |
and (&&), or (||), and not (!) |
13. |
print "Thtring, pleathe!: " user_input = gets.chomp user_input.downcase! if user_input.include? "s" user_input.gsub!(/s/, "th") else puts "Nothing to do here!" end puts "Your string is: #{user_input}" =========================================================== 同上“l”則(/l/,”th”) |
LOOPING WITH RUBY |
|
14. |
counter = 1while counter < 11 puts counter counter = counter + 1end |
i = 0until i == 6 i += 1endputs i | |
15. |
for num in 1..20 puts numend1..20 1~201…20 1~19 |
16. |
i = 0 loop do i += 1 print "#{i}" break if i > 5 end |
|
for i in 1..5 next if i % 2 == 0 print Iend | ||
17. |
array = [1,2,3,4,5] array.each do |x| x += 10 print ("#{x}\n") end |
output:11~15 |
18. |
1000.times { print "Miku! " } |
Miku!印100次 |
19. |
text = gets.chomptext.split(" ") | 把text讀入的輸出字串 output:[“a”,”b”,”c”] |
text = gets.chomptext.split(",") | 把text讀入的整串以陣列輸出 output:[“a b c”] |
|
puts "a:" a = gets.chomp puts "b:" b = gets.chomp words = a.split(" ") words.each do |word| if word != b print word + " " else print "REDACTED " end end |
a: fds gjh we cv b: wef ds gjh REDACTED cv ["fds", "gjh", "we", "cv"] |
|
ARRAYS AND HASHES |
||
20. |
multi_d_array = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] multi_d_array.each { |x| puts "#{x}\n" } |
|
my_hash = { "name" => "Eric", "age" => 26, "hungry?" => true } puts my_hash["name"] puts my_hash["age"] puts my_hash["hungry?"] |
hash = { key1 => value1, key2 => value2, key3 => value3 } |
|
pets = Hash.new people["name"] = "Eric" people["age"] = 26 people["hungry?"] = true puts people["name"] puts people["age"] puts people["hungry?"] |
pets = Hash.new pets["Stevie"] = "cat" # Adds the key "Stevie" with the # value "cat" to the hash |
|
第一是陣列、二跟三是Hashes,反正都是陣列就對了 讀時可用下面兩種方式: |
||
test.each { |x, y| puts "#{x} #{y}" } |
test.each do | x | x.each do | y | puts y end end |
|
|
puts "Text please: " x= gets.chomp x = text.split(" ") y = Hash.new(0) words.each { |x| y[x] += 1 } y = y.sort_by {|a, b| b } y.reverse! y.each { |x, y| puts x + " " + y.to_s } |
把輸入的x字串拆成2微陣列 ###gets.chomp讀入時是字串,要相加要先轉成數字.to_i,但是相對如果要把數字變成字串,要加.to_s。陣列的話是.to_a。 |
https://drive.google.com/file/d/0BzP5BGih9Ax7YjZpYjhHZnVpSWM/view |
||
還為編完XD 話說Word複製貼上格式竟然沒跑掉耶 不過圖都不見了,圖晚點補 WWWWW 如果有錯請提點一下 也歡迎轉載 |