Skip to content

Instantly share code, notes, and snippets.

@Yi-Tseng
Created February 2, 2013 06:06
Show Gist options
  • Save Yi-Tseng/4696281 to your computer and use it in GitHub Desktop.
Save Yi-Tseng/4696281 to your computer and use it in GitHub Desktop.
puts "Hello World!"
10.times do |i|
puts i+1
end
names = ["ruby", "is", "awesome"]
for name in names
puts name
end
names.each do |name|
puts name
end
ages = [18, 25, 46, 53]
sum = 0
ages.each do |age|
sum += age
end
puts sum
puts [18, 25, 46, 53].inject{ |sum, x| sum + x }
puts '中文測試'
END{
puts 'Hello end'
#無論如何這邊都會最晚執行
}
puts 'Hello' if true
BEGIN{
puts 'Hello begin'
#無論如何這邊都會先做
}
#如果a還沒設定(false 或是 nil)則把它設定為1
a ||= 1
puts a
10.upto(15) do |i|
puts i
end
10.downto(5) do |i|
puts i
end
10.times {|i|
puts i
}
#global var
$globalVariable = 1
#class
class Test
@objectVar = 1
@@classVar = 1
def initialize
# constructure
end
end
# swap
a = 1
b = 2
puts a
puts b
a, b = b, a
puts a
puts b
puts 1.class
puts 10%4
a = "abcd\nefg" # 會解析,速度較慢
b = 'abcd\nefg' # 不會解析,速度快
puts a
puts b
a = "I'm \"Eric\""
puts a
a = %Q(I'm "Eric") # %Q()等同於雙引號的效果
puts a
a = %Q{I'm "Eric"} # %Q()等同於雙引號的效果(只要有對稱得號就可以用!)
puts a
a = %Q|I'm "Eric"| # %Q()等同於雙引號的效果(只要有對稱得號就可以用!)
puts a
a = %Q/I'm "Eric"/ # %Q()等同於雙引號的效果(只要有對稱得號就可以用!)
puts a
a = %Q@I'm "Eric"@ # %Q()等同於雙引號的效果(只要有對稱得號就可以用!)
puts a
a = %Q$I'm "Eric"$ # %Q()等同於雙引號的效果(只要有對稱得號就可以用!)
puts a
b = %q(I'm 'Eric') # %q同上
puts b
a = 1
:abc = 2 # 前面加上一個帽後視同 symbol ,可是同系統唯一的符號,在任何地方用都會是相同的物件!
text = %Q{"At vero eos et accusamus et iusto odio dignissimos ducimus qui
blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et
quas molestias excepturi sint occaecati cupiditate non provident, similique
sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum
fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero
tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus
id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis
dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum
necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non
recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis
voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat."}
puts text.delete(' ').delete('.').delete("\n").delete(',').delete('"').length
puts text.split(' ').size
# 字串反向
a = 'abcdefg'
puts a.reverse
# 同上
new_str=[]
a.split("").each do |c|
new_str.unshift c
end
puts new_str.join
# 另一種建立 array 的方式
arr1 = %w(this is a book)
arr2 = %w(1 2 3 4)
p arr1
p arr2
array = [1,3,5,1,7,nil,7]
p array.compact.uniq.sort
# compact 移除 nil 元素
# uniq 移除相同的
map 這樣用
arr = [1, 2, 3, 4, 5]
p arr.map { |e| e*2 }
p arr.map { |e| e*2-1 }
p (1..10).to_a # 直接產生 1~10 的陣列
p (1...10).to_a # 直接產生 1~9 的陣列
deck = (1..52).to_a
deck = deck.shuffle
p deck[0,5] # 從第 0 個位址拿 5 個
p deck[0..4] #拿取 0, 1, 2, 3, 4 個元素
p deck[0...5] #同上
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment