ruby学习笔记之语法篇


    最近开始对ruby有点兴趣。很喜欢这种语言风格,和灵活的编程乐趣,另外工作之余有些时间,于是开始更新一年多没有更新的博客。
     ruby学习笔记的内容比较简洁,为学习后总结出来的要点,大部分会附上知识点的测试代码和运行结果。后续会不断更新。
     笔记内容按照学习步骤:先是ruby语法,后面会开始ruby on Rails的学习。


1、关键字、标识符、注释

关键字 nil false 为假
标识符:(首字符辅助指定作用域)1、局部变量 (小写字母下划线) 2、全局变量 $ 3、实例变量 @ 4、类变量 @@ 5、常量与类 大写字母
注释 # 或=begin =end

2、数据与运算

  2.1常量 变量  :常量值可修改  大写开头  

  2.2基本类型 :数字、字符串、数组、符号、散列表、区间、正在表达式。

3.流程控制  if  unless case 

 if XXX [then]
 elsif xxx[then]
 else 
 end    
 xxx if

unless [then]
else 
end
xxx unless

case xxx
when xx,xx ,xx
else 
end

4、循环控制 while  until for each

while xxx do
end
until xx do
end
for xx in .. do
end
...each do |i|
end
控制 break next redo retry

5.oop

Object=>Module=>Class=>xxx
类 new initialize  不要定义new方法

类方法与属性定义 attr attr_reader attr_writer attr_accessor 其中attr(:attr1,true)等价于attr_accessor :attr1
attr :attr1 等价于attr_reader :attr1
class Student
    attr_accessor :attr1
    public
    def self.method1
    end
    protected
    def Student.method2
    end
     private
    def method3
     end
    def attr1
         @attr1
    end
    def attr1=(value)
         @ttr1=value
    end
end
private 与protected的区别:都只能在类或子类内部使用,区别protected可在类或子类内部被本类其他对象使用

类的扩展和继承

(1)、扩展类

     class Fixnum
          def say_hello
                   puts "hello"
          end
          #覆盖旧方法
          def abs
               puts "new abs method"
          end
     end
#扩展类方法
     def Fixnum.say_helloworld
          puts "say helloworld"
     end
#批量扩展类方法
class << Fixnum
     def  say_hi
          puts "hi"
     end
end
1.say_hello  #hello
Fixnum.say_helloworld #say helloworld
Fixnum.say_hi #hi

(2)、继承   <    super调用父类方法 

6、别名

     alias  新名字 旧名字
     新名字指向原有方法的一个备份,原有方法被重写或覆盖,新名字调用的依然是原有方法
     class Fixnum
          alias add +
          def +(b)
               return self.add(b*4)
          end
     end
     puts "#{1+1}"   #5

7、复制和冻结对象

     简单对象(数字、字符串)赋值时会自动复制操作
     clone与dup区别:clone的还能使用与对象相关联的方法。
     这两种方法都为浅复制,不会复制对象中包含的其他对象
     冻结对象 freeze   试图修改会报TypeError的错误,但可以修改引用
     a=“text”
     a.freeze
     a << "ss" #TypeError
     a=a+"ss"  #textss

8、对象序列化

     Marshal.dump      Marshal.load

9、模块和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_method
             method_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.new
           puts a.class   #Array

          根据实例变量名字设置和获取实例中实例变量的值
           instance_variavle_get  instance_variable_set
            ex:class Student
          def initialize
           @age
                   end
                    end
                s=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 Student
def Student.new_method(name,&block)
define_method(name,&block)
end
end
Student.new_method(:say_hello){puts "say_hello"}
s=Student.new()
s.say_hello
3.9.5 const_missing、method_missing
1.未定义截获
2、动态定义常量和方法

12动态删除定义

        remove_method  undef_mthod
 remove_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  end
yield
ex:
<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会退出调用的地方
异常处理
begin
         rescue  Exception1
         rescue => err
         ensure
         end
 
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值