david_ng223 发表于 2014-2-2 17:56
需要注意的是,Ruby 里面的除法和取余数和C 什
么的不一样,它是对 -無限(原本沒有"無限"這兩個字,是橫型 ...
print 输出对象 输出结果为字符串 如果对象不是字符串 转换为字符串再输出 调用的是to_s
假设有语句print 1,"Hello",[3,4,"ok"],:symbol,Actor.new
print 1,"Hello",[3,4,"ok"],:symbol,Actor.new
就相当于print 1.to_s,"Hello".to_s, [3,4,"ok"].to_s,:symbol.to_s,Actor.new.to_s
print 1.to_s,"Hello".to_s, [3,4,"ok"].to_s,:symbol.to_s,Actor.new.to_s
有个例外就是 当参数为nil时 输出"nil"
print的结果看不到双引号
puts 输出参数中的对象以及一个换行符,如果没有参数则输出一个换行符 如果参数是一个数组,那么数组中的元素会按照顺序进行输出,如果是一个数组和字符串以外类型的对象,函数会先用to_ary方法转换为数组,然后用to_s方法转换为字符串再进行输出,如果最终所得的是nil,那么它也会输出nil
假设有语句puts 1,"Hello",[3,4,"ok"],:symbol,Actor.new
puts 1,"Hello",[3,4,"ok"],:symbol,Actor.new
就相当于print 1.to_s+"\n"+"Hello".to_s+"\n"+[3,4,"ok"].to_s+"\n"+:symbol.to_s+"\n"+Actor.new.to_s+"\n"
print 1.to_s+"\n"+"Hello".to_s+"\n"+[3,4,"ok"].to_s+"\n"+:symbol.to_s+"\n"+Actor.new.to_s+"\n"
p 将参数以人类可读的形式输出 调用的是inspect
假设有语句p 1,"Hello",[3,4,"ok"],:symbol,Actor.new
p 1,"Hello",[3,4,"ok"],:symbol,Actor.new
就相当于print 1.inspect+"\n"+"Hello".inspect+"\n"+[3,4,"ok"].inspect+"\n"+:symbol.inspect+"\n"+Actor.new.inspect+"\n"
print 1.inspect+"\n"+"Hello".inspect+"\n"+[3,4,"ok"].inspect+"\n"+:symbol.inspect+"\n"+Actor.new.inspect+"\n"
|