最近开始对ruby有点兴趣。很喜欢这种语言风格,和灵活的编程乐趣,另外工作之余有些时间,于是开始更新一年多没有更新的博客。ruby学习笔记的内容比较简洁,为学习后总结出来的要点,大部分会附上知识点的测试代码和运行结果。后续会不断更新。笔记内容按照学习步骤:先是ruby语法,后面会开始ruby on Rails的学习。
1、关键字、标识符、注释
关键字 nil false 为假标识符:(首字符辅助指定作用域)1、局部变量 (小写字母下划线) 2、全局变量 $ 3、实例变量 @ 4、类变量 @@ 5、常量与类 大写字母注释 # 或=begin =end2、数据与运算
2.1常量 变量 :常量值可修改 大写开头
2.2基本类型 :数字、字符串、数组、符号、散列表、区间、正在表达式。
3.流程控制 if unless case
if XXX [then]elsif xxx[then]elseendxxx if
unless [then]elseendxxx unless
case xxxwhen xx,xx ,xxelseend4、循环控制 while until for each
while xxx doenduntil xx doendfor xx in .. doend...each do |i|end控制 break next redo retry5.oop
Object=>Module=>Class=>xxx类 new initialize 不要定义new方法
类方法与属性定义 attr attr_reader attr_writer attr_accessor 其中attr(:attr1,true)等价于attr_accessor :attr1attr :attr1 等价于attr_reader :attr1class Studentattr_accessor :attr1publicdef self.method1endprotecteddef Student.method2endprivatedef method3enddef attr1@attr1enddef attr1=(value)@ttr1=valueendendprivate 与protected的区别:都只能在类或子类内部使用,区别protected可在类或子类内部被本类其他对象使用类的扩展和继承
(1)、扩展类
class Fixnumdef say_helloputs "hello"end#覆盖旧方法def absputs "new abs method"endend#扩展类方法def Fixnum.say_helloworldputs "say helloworld"end#批量扩展类方法class << Fixnumdef say_hiputs "hi"endend1.say_hello #helloFixnum.say_helloworld #say helloworldFixnum.say_hi #hi(2)、继承 < super调用父类方法
6、别名
alias 新名字 旧名字新名字指向原有方法的一个备份,原有方法被重写或覆盖,新名字调用的依然是原有方法class Fixnumalias add +def +(b)return self.add(b*4)endendputs "#{1+1}" #57、复制和冻结对象
简单对象(数字、字符串)赋值时会自动复制操作clone与dup区别:clone的还能使用与对象相关联的方法。这两种方法都为浅复制,不会复制对象中包含的其他对象冻结对象 freeze 试图修改会报TypeError的错误,但可以修改引用a=“text”a.freezea << "ss" #TypeErrora=a+"ss" #textss8、对象序列化
Marshal.dump Marshal.load9、模块和Mixin
(1)模块
module 无实例对 象的概念。可以用.或::引用模块方法和变量。被类包含时,模块方法变为类方法(2)加载和混入模块
require与load:require只加载一次include和extend:include 模块,模块方法变为实例方法extend相当于self.extend self为类即为类方法,为实例即为实例方法10、动态特性
(1)动态执行代码
eval class_eval module_eval instance_eval对象的send方法(2)动态获得类或模块中的方法、常量或变量的值
method instance_method protected_method protected_instance_methodmethod_defined? respond_to?const_get根据常量名获得模块或类中常量的值str="PI"puts Math.const_get(str) #3.141592653使用const_get根据类名创建类的实例str="Array"array=Object.const_get(str)a=array.newputs a.class #Array
根据实例变量名字设置和获取实例中实例变量的值instance_variavle_get instance_variable_setex:class Studentdef initialize@ageendends=Student.new()puts s.instance_variable_get("@age") #s.instance_variable_set("@age",10)puts s.instance_variable_get("@age") #10
11、动态定义方法
define_method模块的私有方法,类中可以为其提供接口,define_method(name,&block)class Studentdef Student.new_method(name,&block)define_method(name,&block)endendStudent.new_method(:say_hello){puts "say_hello"}s=Student.new()s.say_hello3.9.5 const_missing、method_missing1.未定义截获2、动态定义常量和方法12动态删除定义
remove_method undef_mthodremove_method只会删除当前的方法 undef_method连父类的方法也会删除remove_const都为Module类的方法remove_method remove_const都为Module的私有方法,只能在类或模块的内部使用ex:class Person NAME="animal" WORDS="hello" def method1 puts "person method1" end def method2 puts "person method2" end end class Student < Person def method1 puts "student method1" end def method2 puts "student method2" end remove_method :method1 undef_method :method2 end s=Student.new() s.method1() #person method1 s.method2() #undefined method
代码块和迭代1、代码块定义 {} do endyieldex:<span style="white-space:pre"> </span><pre name="code" class="ruby">class Array def do_each for i in (0..self.length()-1) yield self[i] end end end say="" (["a","b","c","d"]).do_each do |word| say+=word puts say end
2、Proc和lambda
不同处,lambda和调用方法的方式相同 return只退出代码块Proc调用相当于代码块写在block.call这里,return会退出调用的地方异常处理beginrescue Exception1rescue => errensureend
ruby学习笔记之语法篇
最新推荐文章于 2024-04-27 11:27:11 发布