Ruby语法基础(一)

后端开发   发布日期:2025年07月11日   浏览次数:117

Ruby是一种开源的面向对象程序设计的服务器端脚本语言,最初由松本行弘(Matz)设计开发,追求『快乐和生产力』,程序员友好型,被称为『human-oriented language』

hello world

首先打开vim

  1. $ vim hello.rb

编写helloworld代码

  1. puts "hello world!"

保存后运行

  1. $ ruby hello.rb
  2. hello world! # 运行结果

ruby也可以在命令行中键入irb,打开命令行交互模式,类似中Python的shell

Here Document

  1. puts <<EOF
  2. 这是一种here document
  3. 是一种多行字符串
  4. 可以用来写大段文档
  5. EOF

Begin&END

  1. puts '这是主Ruby脚本'
  2. # end语句会在程序的结尾被调用
  3. END{
  4. puts '停止Ruby程序'
  5. }
  6. # begin语句会在程序的开始被调用
  7. BEGIN{
  8. puts '初始化Ruby程序'
  9. }

注释

  1. =begin
  2. 单行注释用#
  3. 多行注释用=begin .... =end
  4. =end
  5. puts '测试注释'

基本数据类型

数值类型Number

  1. a = 123_123 # 十进制可以加下划线
  2. b = 0x377 # 十六进制表示
  3. c =0b1011 # 二进制表示
  4. d = 6.32E-32 # 科学记数法表示
  5. puts a, b, c, d

字符串类型(String)

  • 单引号中的内容不会被转义
  • 双引号中的内容会被转义,#{}内的脚本会被执行
  1. name = 'world'
  2. puts 'hello\tname'
  3. puts "hello\t#{name.upcase}"
  4. puts "24*20*35=#{24*20+35}" # 计算并转成字符串
  5. 运行结果:
  6. hello\tname
  7. hello WORLD
  8. 24*20*35=515

#{ expr }可以执行expr脚本并转换成字符串

数组(Array)

类似于Python中的list,支持[]索引,插入,删除,替换,集合间运算

  1. a = ['fred',
  2. 10,
  3. 3.14,
  4. 'String',
  5. 'last'
  6. ]
  7. a.each do |i|
  8. print i,"!\n"
  9. end

哈希(Hash or Dictionary)

  1. hsh = {
  2. :red=>0xf00, # 字符串作为key的情况下,用symbol比较好
  3. :green => 0x0f0,
  4. :blue => 0x00f
  5. }
  6. hsh.each do |key, value|
  7. print key, ' is ', value, "\n"
  8. end

范围(Range)

  1. (10..15).each do
  2. |i|
  3. print i, ' '
  4. end

类和对象

  1. # 定义类
  2. class Customer
  3. # 类变量
  4. @@nu_of_customer = 0
  5. def initialize(id, name, addr)
  6. # 属于实例本身的实例变量
  7. @cust_id = id
  8. @cust_name = name
  9. @cust_addr = addr
  10. @@nu_of_customer += 1
  11. end
  12. # 显示细节的函数
  13. def show
  14. puts "Customer id #{@cust_id}"
  15. puts "Customer name #{@cust_name}"
  16. puts "Customer addr #{@cust_addr}"
  17. end
  18. def show_total
  19. puts "Total number of customers: #@@nu_of_customer"
  20. end
  21. end
  22. # 创建对象
  23. cust1 = Customer.new('1','John','Wisdom Apartments, Ludhiya')
  24. cust2 = Customer.new('2','Poul','New Empire road, Khandala')
  25. # 调用方法
  26. cust1.show
  27. cust2.show
  28. cust1.show_total
  29. 运行结果
  30. Customer id 1
  31. Customer name John
  32. Customer addr Wisdom Apartments, Ludhiya
  33. Customer id 2
  34. Customer name Poul
  35. Customer addr New Empire road, Khandala
  36. Total number of customers: 2

变量

全局变量

以$开头,不建议使用

  1. def f1
  2. $global_variable += 1
  3. puts "f1: $global_variable = #{$global_variable}"
  4. end
  5. # 全局变量用$开头
  6. $global_variable = 0
  7. puts "main: $global_variable = #{$global_variable}"
  8. f1
  9. puts "main: $global_variable = #{$global_variable}"
  10. 运算结果
  11. main: $global_variable = 0
  12. f1: $global_variable = 1
  13. main: $global_variable = 1

实例变量

以@开头,属于实例本身,另外ruby体现数据与操作结合,外部不可以直接修改实例变量本身,博主认为这个面向对象很彻底

  1. class Sample
  2. def initialize
  3. @x = 0 # 初始化后使用
  4. end
  5. def get_x
  6. @x
  7. end
  8. def set_x(x)
  9. @x = x
  10. end
  11. end
  12. s = Sample.new
  13. s.set_x 2 # 命令行的传参风格
  14. puts s.get_x

类变量

类变量以@@开头,必须初始化后使用。类变量在定义它的类或模块的子类的子类或子模块中可以共享,例子上面讲类和对象时已经指出

局部变量

局部变量以小写字母或者_开头,生命周期从class、module、def或do到相应的end,或者两个括号之间。一旦出作用域就直接释放

  1. def sample_func
  2. x = 1
  3. puts x
  4. end
  5. sample_func
  6. # puts x 报错undefined local variable or method `x' for main:Object (NameError)

常量

  • 常量用大写字母开头
  • 定义在类或模块内的常数可以从类或模块的内部访问,定义在类或模块外的常量可以被全局访问
  • 常量不能定义在方法内部
  1. class Example
  2. VAR1 = 100
  3. VAR2 = 200
  4. def show
  5. puts "VAR1=#{VAR1}"
  6. puts "VAR1=#{VAR2}"
  7. end
  8. end
  9. obj = Example.new
  10. obj.show
  11. 运行结果
  12. VAR1=100
  13. VAR1=200

伪变量

类似于pyhton或PHP中的魔法常量,只能读不能写

  • self: 当前方法的接收器对象。
  • true:代表 true 的值。
  • false: 代表 false 的值。
  • nil: 代表 undefined 的值。
  • __FILE__: 当前源文件的名称。
  • __LINE__: 当前行在源文件中的编号

小结

本文也就对于ruby有了快速的感性认识,进一步的学习还要继续学习

参考教程菜鸟教程

以上就是Ruby语法基础(一)的详细内容,更多关于Ruby语法基础(一)的资料请关注九品源码其它相关文章!