zoukankan      html  css  js  c++  java
  • 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. 
    "
    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
    "
    puts ip
    puts ip.gsub!(/(
    *)$/, '')
    
    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?方法两者表达的意思是一样的。
    
    
    [Haima的博客] http://www.cnblogs.com/haima/
  • 相关阅读:
    mysql查看每张表的空间使用情况
    下一步开发的技术点
    技术体系需要继续探索的东西
    架构体系需要进一步研究探索的V2路线图
    串行写队列的MYSQL大文本参数
    Node.js 数据存储方式的选择
    Node.js npm 详解
    Node入门
    Node.js知识点学习
    为什么应使用 Node.js
  • 原文地址:https://www.cnblogs.com/haima/p/14771097.html
Copyright © 2011-2022 走看看