Ruby语法基础(二)

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

继续ruby的学习,这次主要把目光放到运算符,条件判断,循环,方法,以及其他ruby特有的基本概念上

运算符

  • 算术运算符:+,-,/,%,**,值的注意的是,ruby中一切皆为对象,a+b等价于a.+(b)
  • 比较运算符:基本与Python的一致,不等于要用!=,还有联合运算符<=>,返回-1,0,1。.eql?判断数据值是否相等以及数据类型是否相等。equal?判断两个对象的id是否相等
  • 赋值运算:与Python一致,可以用a, b, c = 10, 20, 30并行赋值,交换也可以直接运行
  • 位运算:与Python一致,对二进制位操作,支持&, |,^,~(取反),<<,>>
  • 三元运算符: ? :
  • 范围运算符: 两个.包含结尾位置1..3(1,2,3);三个点不包括结尾1…3(1,2)
  • defined?:判断是否已经定义
  1. foo = 42; $VAR = 1
  2. # 1.判断变量是否已经初始化
  3. puts defined? foo # local-variable
  4. puts defined? $VAR # global-variable
  5. puts defined? bar # 未定义
  6. # 2.判断方法是否已经定义
  7. puts defined? puts # method

条件判断

if…else

  1. x = 1
  2. if x>2
  3. puts 'x > 2'
  4. elsif x<=2 and x!=0
  5. puts 'x = 1'
  6. else
  7. puts 'unknow x'
  8. end

if修饰词

if后的条件成立才执行前面一句(比较灵活的写法)

  1. a = 1
  2. print a if a # 变量a被定义过了,所以返回true

unless

与if刚好相反

case

  1. age = 5
  2. case age
  3. when 0..2
  4. puts '婴儿'
  5. when 3..6
  6. puts '小孩'
  7. when 7..12
  8. puts '少年'
  9. else
  10. puts '其他'
  11. end

循环

while

  1. i = 0; num = 5
  2. while i<num
  3. puts i; i += 1
  4. end

while修饰符

  1. i = 0; num = 5
  2. begin
  3. puts i; i += 1
  4. end while i<num

until

until与while完全相反

for

  1. for i in 0..5
  2. if i == 2
  3. next # 相当于continue
  4. elsif i == 4
  5. break # 跳出循环
  6. end
  7. puts i
  8. end

方法

  • 方法名必须以小写开头,否则会被当成常量
  • Ruby 中的每个方法默认都会返回一个值。这个返回的值是最后一个语句的值
  • return 语句用于从显示从方法中返回一个或多个值
  1. def test(a=1, b) # 可以使用默认参数
  2. return a+b, a-b
  3. end
  4. sum, sub = test 2, 3
  5. puts sum, sub
  • 不定长参数
  1. def sample (*test) # *号允许可变数量的参数
  2. test.each do
  3. |item|
  4. puts item
  5. end
  6. end
  7. sample 'a', 'b'
  8. sample 1, 2, 3
  • 类方法
  1. class Accounts
  2. def reading_charge
  3. # dosomething
  4. end
  5. def Accounts.return_date
  6. # 类方法
  7. end
  8. end
  9. Accounts.return_date # 类方法调用
  • alias可以重命名
  1. alias 方法名 方法名
  2. alias 全局变量 全局变量
  • undef取消定义
  1. undef 方法名

块在ruby中用{}包裹,也可以用do end形式

  1. # 定义函数
  2. def myloop
  3. while true
  4. yield
  5. end
  6. end
  7. num = 1
  8. # 执行myloop函数时,会把块中内容挂载到yield处
  9. myloop do
  10. puts "num is #{num}"
  11. break if num > 100
  12. num *= 2
  13. end
  14. # 运行结果
  15. num is 1
  16. num is 2
  17. num is 4
  18. num is 8
  19. num is 16
  20. num is 32
  21. num is 64
  22. num is 128

传递参数

  1. def total(from, to)
  2. result = 0
  3. from.upto(to) {
  4. |num|
  5. if block_given?
  6. result += yield num
  7. else
  8. result += num
  9. end
  10. }
  11. result
  12. end
  13. puts total 1, 3
  14. puts total(1,3) { # total后一定要紧跟括号
  15. |num|
  16. num ** 2
  17. }

块本身也可以作为参数传递给函数(不实用yield方法)

  1. # 块本身也可以作为参数传递给函数
  2. def test(&block)
  3. block.call
  4. end
  5. test {
  6. puts 'Hello World!'
  7. }

模块

模块可以把方法、常量、模块组合在一起

  • 模块提供了一个命名空间,避免了名字冲突
  • 模块实现了mixin装置
  • 模块不能实例化
  • 模块没有子类
  • 模块只能被另一个模块定义

被引用的模块文件support.rb

  1. module Support
  2. PI = 3.1415926
  3. def Support.hello(name) # 模块中的普通函数用模块名开头区分
  4. puts "hello #{name}"
  5. end
  6. end
  7. module Week
  8. FIRST_DAY = 'Sunday'
  9. def Week.weeks_in_month
  10. puts 'You have 4 weeks in a month'
  11. end
  12. def Week.weeks_in_year
  13. puts 'You have 52 weeks in a year'
  14. end
  15. end

main.rb

  1. $LOAD_PATH << '.' # 添加搜索路径目录
  2. require 'support' # 添加具体模块
  3. # 调用模块方法
  4. Support::hello 'ruby'
  5. # 读取模块函数
  6. puts Support::PI
  7. # 在类中引用模块内容要用include
  8. class Decade
  9. def initialize
  10. @no_of_yrs = 10
  11. end
  12. # include Week
  13. def no_of_months
  14. puts Week::FIRST_DAY
  15. number = @no_of_yrs * 12
  16. puts number
  17. end
  18. end
  19. d = Decade.new
  20. puts Week::FIRST_DAY
  21. Week.weeks_in_month
  22. Week.weeks_in_year
  23. d.no_of_months
  24. #运算结果
  25. hello ruby
  26. 3.1415926
  27. Sunday
  28. You have 4 weeks in a month
  29. You have 52 weeks in a year
  30. Sunday
  31. 120

mixins实现多重继承

support.rb

  1. module A
  2. def a1
  3. puts 'a1'
  4. end
  5. def a2
  6. puts 'a2'
  7. end
  8. end
  9. module B
  10. def b1
  11. puts 'b1'
  12. end
  13. def b2
  14. puts 'b2'
  15. end
  16. end

main.rb

  1. $LOAD_PATH << '.' # 添加搜索路径目录
  2. require 'support' # 添加具体模块
  3. class Sample
  4. # 用 include方法把模块中的方法包含到类内
  5. # 组装模块
  6. include A
  7. include B
  8. def s
  9. puts 's'
  10. end
  11. end
  12. s = Sample.new
  13. s.a1 # 调用模块中的方法
  14. s.a2
  15. s.b1
  16. s.b2
  17. s.s

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