Ruby
解释执行
无需声明变量
每条代码都有返回值
纯面向对象,“真一切皆为对象”
简洁的判断语句:order.calculate_tax unless order.nil?
鸭子类型
符号
1# :string 表示 symbol,:string.object_id == :string.object_id2def tell_the_truth(options={})3 if options[:profession] == :lawyer4 'it could be believed that this is almost certainly not false.'5 else6 true7 end8end910tell_the_truth11# => true12tell_the_truth :profession => :lawyer13# => "it could be believed that this is almost certainly not false."14a = {:profession => :lawyer, :string => 'hahha'} # 散列表15tell_the_truth a16# => "it could be believed that this is almost certainly not false."
代码块(匿名函数)
1animals = ['lions', 'tigers', 'bears', 'duck']2animals.each {|a| puts a}3# lions4# tigers5# bears6# duck7# => ["lions", "tigers", "bears", "duck"]
yield 代码块
1class Fixnum2 def my_times3 i = self4 while i > 05 i = i - 16 yield7 end8 end9end10113.my_times {puts 'mangy moose'}12# mangy moose13# mangy moose14# mangy moose15# => nil
&block 闭包
1def call_block(&block)2 block.call3end45def pass_block(&block)6 call_block(&block)7end89pass_block {puts 'helloo'}10# helloo11# => nil
类
14.class.superclass.superclass.superclass.superclass2# Integer Numeric Object BasicObject nil344.class.class.superclass.superclass.superclass.superclass5# Integer Class Module Object BasicObject nil
1class Tree2 attr_accessor :children, :node_name34 def initialize(name, children = [])5 @children = children6 @node_name = name7 end89 def visit_all(&block)10 visit &block11 children.each {|c| c.visit_all &block}12 end1314 def visit(&block)15 block.call self16 end17end1819ruby_tree = Tree.new("Ruby", [Tree.new("Reia"), Tree.new("MacRuby")])20ruby_tree.visit {|node| puts node.node_name}21# Ruby22ruby_tree.visit_all {|node| puts node.node_name}23# Ruby24# Reia25# MacRuby
Mixin 多继承:Java 通过接口实现,Ruby 通过 Mixin,先定义类的主要部分,然后用模块添加额外功能
1module ToFile2 def to_f3 puts "mixin tofile"4 end5end67class Person8 include ToFile9 attr_accessor :name1011 def initialize(name)12 @name = name13 end1415 def to_s16 name17 end18end1920Person.new('matz').to_f21# mixin tofile
可枚举(枚举模块):让类可枚举,必须实现 each
1a = [5, 2, 4, 3, 1]2b = a.sort # [1, 2, 3, 4, 5]3a.any? {|i| i > 4} # true4a.all? {|i| i > 4} # false5c = a.collect {|i| i * 2} # [10, 4, 6, 8, 2]6d = a.select {|i| i % 2 == 0} # [2, 4]7a.member?(2) # true8a.inject(10) do |acc, cur|9 puts "acc: #{acc}, cur: #{cur}"10 acc + cur11end12# => 25
可比较(比较模块):让类可比较,必须实现 <=>
1'same' <=> 'same' # 0
元编程:写能写程序的程序
1# rails2class Department < ActiveRecord::Base3 has_many :employees4 has_one :manager5end
开放类:可以对类重新定义
1class NilClass2 def blank?3 true4 end5end67class String8 def blank?9 self.size == 010 end11end1213["", "person", nil].each do |e|14 puts e unless e.blank?15end16# "person"
method_missing:找不到默方法时调用,可以复写 method_missing 方法
1class Roman2 def self.method_missing name, *args3 roman = name.to_s4 roman.gsub("IV", "IIII")5 roman.gsub("IX", "VIIII")6 roman.gsub("XL", "XXXX")7 roman.gsub("XC", "LXXXX")89 (roman.count("I") +10 roman.count("V") * 5 +11 roman.count("X") * 10 +12 roman.count("L") * 50 +13 roman.count("C") * 100)14 end15end1617puts Roman.XII # 12
动态的改变类:模块被另一模块包含,Ruby 就会调用该模块的 included 方法,类也是模块
feeling
自由(有点像 JS 中修改原型,但平时更多是限制的)