def say_goodnight(name)
"Good night, #{name.capitalize}"
end
puts say_goodbye("ma")
- #{expression} #{}中是一个表达式,不仅可是一个变量
- 如果没有return ,则返回的是最后一个被求值的表达式的值
a = %w{ant bee dog cat} #建立全部由单词组成的数组
#相当于
a = [ 'ant', 'bee', 'dog', 'cat' ]
在hash中使用symbol
pet_name= {
:cat => 'sumer',
:dog => 'lucky'
}
#相当于
pet_name = {
cat: 'summer',
dog: 'lucky'
}
Blocks and Iterators
[ 'cat', 'dog', 'horse'].each{ |name| print name," "}
t.times [print "*"}
3.upto(6) { |i| print i}
('a'..'e').each{ |char| print char}
Ruby有和C相似的printf语句
读取输入
while line=gets
print line
end