Ruby类方法 实例方法
2014-05-02 11:27
190 查看
ruby的类方法与实例方法
类方法也叫静态方法,通过类名来调用的方法。实例方法,必须要new一个实例出来才能用。class Foo def self.bar puts 'class method' end def baz puts 'instance method' end end Foo.bar #class method #Foo.baz #报错 undefined method `baz' for Foo:Class (NoMethodError) Foo.new.baz #instance method #Foo.new.bar #报错 undefined method `bar' for # (NoMethodError)
当中bar就是类方法,看它是如何定义的:def self.bar,self就是指向当前的类。而对于实例方法,就很简单:def baz。
像ruby这样灵活的脚本语言不多见,它提供了多种定义类方法的手段。
# Way 1 class Foo def self.bar puts 'class method' end end Foo.bar # "class method" # Way 2 class Foo class << self def bar puts 'class method' end end end Foo.bar # "class method" # Way 3 class Foo; end def Foo.bar puts 'class method' end Foo.bar # "class method"
第一种与第三种方式不细说了,self的运用就相当于javascript的this。第二种有种自继承的意味。通过我们添加多个类方法时就少写几个self,非常优雅。
再看实例方法,这也有几套方案:
# Way 1 class Foo def baz puts 'instance method1' end end Foo.new.baz # "instance method1" puts '---------------' # Way 2 class Foo attr_accessor :baz end foo = Foo.new foo.baz = 'instance method2' puts foo.baz# "instance method2" puts '---------------' # Way 3 class Foo; end foo = Foo.new def foo.lazy puts 'instance method3' end foo.lazy # "instance method3"
第一种直接定义,第二种用到attr_accessor语法糖,第三种是极晚绑定,此方法只对那一个实例有效。
#类的实例对象的方法,方法属于类所New出来的实例对象。
p a.methods.length
p a.class.instance_methods.length
p A.instance_methods.length
p a.public_methods.length
p a.class.public_instance_methods.length
#增加a实例的单件方法
代码
def a.tell
end
p a.methods.length
p a.class.instance_methods.length
p A.instance_methods.length
p a.public_methods.length
p a.class.public_instance_methods.length
输出:51 50 51 50
说明:类的instance_methods包括的只有他所拥有的实例方法,并不包含单件类的方法。并且methods方法其实和Public_methods一样的,有别于private_methods
现在说一下,method方法
这个方法属于Method类,最常用的就是检查方法的参数个数,如下:
class A
def self.ask1(n1,n2)
puts "the method of class"
end
def ask2(n1)
puts "the method of instance"
end
end
a=A.new
p a.method(:ask2).arity
p A.method(:ask1).arity
输出:1 2
class A def initialize @a=0;@c=3 end attr_accessor :a, :c, :b end a = A.new ins_vars = a.instance_variables.map{|v|v.to_s[1..-1]} methods = a.methods.map &:to_s ins_vars & methods #attribute
The
mapmethod
takes an enumerable object and a block, and runs the block for each element, outputting each returned value from the block (the original object is unchanged unless you use
map!):
[1, 2, 3].map { |n| n * n } #=> [1, 4, 9]
Arrayand
Rangeare
enumerable types.
mapwith
a block returns an Array.
map!mutates
the original array.
Where is this helpful, and what is the difference between
map!and
each?
Here is an example:
names = ['danil', 'edmund'] # here we map one array to another, convert each element by some rule names.map! {|name| name.capitalize } # now names contains ['Danil', 'Edmund'] names.each { |name| puts name + ' is a programmer' } # here we just do something with each element
The output:
Danil is a programmer Edmund is a programmer
相关文章推荐
- Ruby类实例变量、类实例方法和类变量、类方法的区别
- 禁止创建SQLSERVER实例的解决方法
- 动态加载jQuery的两种方法实例分析
- jQuery中clone()方法用法实例
- python3 实例方法,类方法,静态方法
- 通过Assembly类加载程序集,并使用Activator类以工厂模式创建类的实例,并且执行类的方法
- JavaScript函数内部属性和函数方法实例详解
- Java 通过JDBC连接Mysql数据库的方法和实例
- jQuery的one()方法用法实例
- android Color的解析实例方法
- javascript数组遍历的方法实例分析
- php打包zip文件,完整实例(附类,附调用方法),已经测试OK的
- Squid访问控制方法的实例
- PHP栈的定义、入栈出栈方法及基于堆栈实现的计算器完整实例
- 朴素贝叶斯方法的应用实例----基于newsgroup文档集的贝叶斯算法实现
- 会计规定中的:跨月退货处理方法实例
- Don’t Use Accessor Methods in Initializer Methods and dealloc 【初始化和dealloc方法中不要调用属性的存取方法,而要直接调用 _实例变量】
- MySQL死锁问题分析及解决方法实例详解
- Spring4 学习系列之——bean的静态工厂方法 和 实例方法
- Jquery Ajax学习实例3 向WebService发出请求,调用方法返回数据