飞机上阅读pdf的笔记,因为不联网,内容不多而且比较零散,以tips的形式记录
puts local_variables
puts `ls` # 种方式会把字符串命令传给系统执行,并且返回结果
puts `pwd`
puts 'In which city do you stay?'
STDOUT.flush # 把IO流中的数据传给全局对象STDOUT
city = gets.chomp
puts 'The city is '+ city
def oldmtd
'old method'
end
alias newmtd oldmtd #这里用别名newmtd代替了oldmtd
def oldmtd # 新的oldmtd
'old improved method'
end
puts oldmtd
puts newmtd
class Dummy
def method_missing(m, *args)
puts "There's no method called #{m} here --please try again"
end
end
Dummy.new.some_method
a = %w[a,b,c,d]
worlds = 'Learning Ruby - Your one stop guide'
puts worlds.split(' ').reverse.join(' ')
# 运行结果 guide stop one Your - Ruby Learning
# 范围变量的使用(检测是否属于某个范围)
puts (1..10) === 3.14159 # true
puts ('a'..'c') === 'd' # false'
# 两种方式等价
address = {
:name=>'高桥',
:pinyin=>'gaoqiao',
:postal=>'1234567'
}
address = {
name:'高桥',
pinyin:'gaoqiao',
postal:'1234567'
}
# 遍历
address.each {
|key, value|
puts "#{key}:#{value}"
}
puts ARGV
# 运行 ruby hi.rb hello world
# 输出
hello
world