探索Ruby编程的乐趣:学习笔记1(上)

发表时间: 2016-01-06 00:00

Ruby类定义

  1. #!/usr/bin/ruby

  2. class Sample

  3. def hello

  4. puts "Hello Ruby!"

  5. end

  6. end

  7. # 使用上面的类来创建对象

  8. object = Sample. new

  9. object.hello

注意:无参数的函数调用可以省略

初始化方法

初始化方法有一个统一的名字叫 initialize

  1. class Customer

  2. @@no_of_customers=0

  3. def initialize(id, name, addr)

  4. @cust_id=id

  5. @cust_name=name

  6. @cust_addr=addr

  7. end

  8. end

Ruby变量

ruby支持5种类型

  • 一般小写字母、下划线开头:变量(Variable)。

  • $开头:全局变量(Global variable)。

  • @开头:实例变量(Instance variable)。

  • @@开头:类变量(Class variable)类变量被共享在整个继承链中

  • 大写字母开头:常数(Constant)。

变量(就是 局部变量)

变量的打印

变量在打印的时候不能省略 大括号,别的类型变量都可以省略大括号,比如

你这样打印变量是打不出东西的

错误的写法

  1. a=1

  2. b=2

  3. puts "a: #a"

  4. puts "b: #b"

打印结果

  1. a: #a

  2. b: #b

正确的写法

  1. a=1

  2. b=2

  3. puts "a: #{a}"

  4. puts "b: #{b}"

打印结果

  1. a: 1

  2. b: 2

变量的生存周期

变量的生存周期只在方法中,出了方法就没了,所以也只能定义在方法里面,比如

错误的写法

  1. class Test2

  2. a=1

  3. b=2

  4. def printVar

  5. puts "a: #{a}"

  6. puts "b: #{b}"

  7. end

  8. end

  9. hellotest = Test2.new

  10. hellotest.printVar

输出

  1. test.rb:5:in `printVar': undefined local variable or method `a' for #<Test2:0x00000002cf2248> (NameError)

  2. from test.rb:10:in `<main>'

正确的写法

  1. class Test2

  2. def printVar(a,b)

  3. puts "a: #{a}"

  4. puts "b: #{b}"

  5. end

  6. end

  7. hellotest = Test2.new

  8. hellotest.printVar(1,2)

变量的传递

简单类型是值拷贝(字符串也是简单对象,这点跟java不一样)

  1. class Test2

  2. def testPass(a,b)

  3. puts "before add : a: #{a} b: #{b}"

  4. addVar(a,b)

  5. puts "after add : a: #{a} b: #{b}"

  6. end

  7. def addVar(a,b)

  8. a += 1

  9. b += 2

  10. end

  11. end

  12. hellotest = Test2.new

  13. hellotest.testPass(1,2)

输出

  1. before add : a: 1 b: 2

  2. after add : a: 1 b: 2

复杂对象是对象引用

  1. class Obj1

  2. def initialize(a)

  3. @a=a

  4. end

  5. def printVal

  6. puts "a: #@a"

  7. end

  8. def setA(a)

  9. @a=a

  10. end

  11. def getA

  12. return @a

  13. end

  14. end

  15. class Test2

  16. def testPass

  17. testobj = Obj1.new("hello")

  18. a = testobj.getA

  19. puts "before add : a: #{a}"

  20. addVar(testobj)

  21. a = testobj.getA

  22. puts "after add : a: #{a}"

  23. end

  24. def addVar(obj)

  25. obj.setA(obj.getA + " world")

  26. end

  27. end

  28. hellotest = Test2.new

  29. hellotest.testPass

输出

  1. before add : a: hello

  2. after add : a: hello world

实例变量

实例变量的打印

实例变量的打印是可以省略大括号的,比如 #@a 跟 #{@a} 是一回事

实例变量的生存周期

实例变量只能在 initialize 里面被定义。如果想像在java中这样定义是错误的

  1. class LearnInstanceVar

  2. @a=1

  3. def printVar

  4. puts "a: #{@a}"

  5. end

  6. end

  7. test1 = LearnInstanceVar.new

  8. test1.printVar

输出

  1. $ ruby test.rb

  2. a:

正确的定义

  1. class LearnInstanceVar

  2. def initialize(a)

  3. @a=a

  4. end

  5. def printVar

  6. puts "a: #{@a}"

  7. end

  8. end

  9. test1 = LearnInstanceVar.new("hello")

  10. test1.printVar

输出

  1. $ ruby test.rb

  2. a: hello

类似java中的private,但是更严格,连定义的位置都只能放在特定的方法里面

未完待续~

长按指纹即可关注哦!每天都会为你推荐有趣有用的资料!喜欢就分享给更多人吧!