ruby 经常使用代码片断

# 文件的当前目录
puts __FILE__
# string.rb

# 文件的当前行
puts __LINE__ # 6
#文件的当前目录
puts __dir__
#/media/haima/34E401CC64DD0E28/site/ruby/RubyStudy/Lesson02

puts __method__

def debug(param = "")
  puts param
end

debug(123) # 123

# 字符串拼接
puts '字符串拼接----------------------'
puts 'i am ' + 'fish' #i am fish

# 字符串大小写转换
puts '大小写转换----------------------'
puts "我是Ruby, Hello World.".upcase #我是RUBY, HELLO WORLD.
puts "我是Ruby, Hello World.".downcase #我是ruby, hello world.

puts '去除空格----------------------'
str1 = "  我是Ruby, Hello world. \n"
puts str1 #我是Ruby, Hello World.
#去除首部的空格,加!号改变本体
str1.lstrip! #我是Ruby, Hello World.  rstrip
puts str1

str2 = "  我是Ruby, Hello world22... " #  我是Ruby, Hello world11...
# 移除两头的空格,不改变本体
puts str2.strip #我是Ruby, Hello world11...
puts str2 #  我是Ruby, Hello world22...
# 返回 str 的副本,移除了尾随的空格。
puts str2.rstrip #  我是Ruby, Hello world11...
# 返回 str 的副本,移除首部的空格。
puts str2.lstrip #我是Ruby, Hello world11...

puts '运算----------------------'
x, y, z = 12, 36, 72
puts "x 的值为 #{ x }"
puts "x + y 的值为 #{ x + y }"
puts "x + y + z 的平均值为 #{ (x + y + z)/3 }"

puts '去换行符----------------------'
ip = "166.88.134.120\n"
puts ip
puts ip.gsub!(/(\n*)$/, '')

puts '客串拼接----------------------'
name = 'haima'
puts "i am #{name}"
puts "#{name + ",ok"}"

puts '判断空----------------------'
a = "haima"
puts a.empty? # false
puts a.nil? # false
puts a.length # 5

# rails的判断为空.blank
# puts a.blank?
# .blank? 至关于同时知足 .nil? 和 .empty? 。
# railsAPI中的解释是若是对象是:
# false, empty, 空白字符. 好比说: "", " ", nil , [], 和{}都算是blank。 (object.blank? 至关于 object.nil?||object.empty?)。

# rails判断是否存在 present?判断是否存在
# present?方法就是blank?方法的相反,判断是否存在,所以present?方法与!blank?方法二者表达的意思是同样的。