創作內容

0 GP

[程式筆記]Ruby on codecademy Note(1/2)

作者:竹中之想│2015-08-12 00:51:09│巴幣:0│人氣:408
  
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!
                                                                      
  print "What's your last name?"
  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:

  we
f

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
  
  
P.S.這是我在codecademy上學的RUBY筆記
還為編完XD
話說Word複製貼上格式竟然沒跑掉耶
不過圖都不見了,圖晚點補
WWWWW

如果有錯請提點一下
也歡迎轉載
引用網址:https://home.gamer.com.tw/TrackBack.php?sn=2926969
All rights reserved. 版權所有,保留一切權利

相關創作

同標籤作品搜尋:程式|筆記|Ruby|codecademy

留言共 2 篇留言

問天
c語言!?

08-12 19:18

問天
歐,是RUBY歐

08-12 19:20

我要留言提醒:您尚未登入,請先登入再留言

喜歡★ap5552477 可決定是否刪除您的留言,請勿發表違反站規文字。

追蹤私訊切換新版閱覽

作品資料夾

dhreekingdon幸運看見的你
給你一顆紅心~讓你能保有一整天的好心情~祝你有個愉快的一天喲(<ゝω・)~❤看更多我要大聲說昨天22:59


face基於日前微軟官方表示 Internet Explorer 不再支援新的網路標準,可能無法使用新的應用程式來呈現網站內容,在瀏覽器支援度及網站安全性的雙重考量下,為了讓巴友們有更好的使用體驗,巴哈姆特即將於 2019年9月2日 停止支援 Internet Explorer 瀏覽器的頁面呈現和功能。
屆時建議您使用下述瀏覽器來瀏覽巴哈姆特:
。Google Chrome(推薦)
。Mozilla Firefox
。Microsoft Edge(Windows10以上的作業系統版本才可使用)

face我們了解您不想看到廣告的心情⋯ 若您願意支持巴哈姆特永續經營,請將 gamer.com.tw 加入廣告阻擋工具的白名單中,謝謝 !【教學】