---------------------------------------------------------------------------------------------------------------------------------------------------------
#About objects,instance variables,and methods.
对象的实例变量被存储在对象里面,实例方法通过对象的object_id到对象的class里面查找实例法
#methods()
返回该对象的所有能respond_to?的方法
#instance_methods: (Only instance can use them)
返回该对象的所有public,protected的实例方法
#private method
私有方法只能直接调用方法名而且当前self变量的值必须为其声明的对象值
---------------------------------------------------------------------------------------------------------------------------------------------------------
ruby的module主要用于mixin(解决多重继承的问题)扩展方法和用于命名空间
当需要一个被继承或者能实例化的对象时才用class
module的优劣:(待完善)
--------------------------------------------------------------------------------------------------------------------------------------------------------
module MyModule
MyConstant = 'Outer constant'
class MyClass
MyConstant = 'Inner constant'
end
end
静态变量在modules和classes里面存储模式类似于文件夹(modules,classes)与文件(constant)的方式
故在不同文件夹下同名文件也不会冲突。
---------------------------------------------------------------------------------------------------------------------------------------------------------
load() 每次都会重新加载文件,并执行里面的码。
require() 只加载一次,加载的模式相当于import进来一个库。
#当应用load("filename", ture) 时加载进来的文件将放在一个匿名module里面,用完之后销毁匿名modue(目的是阻止加载进入的文件pollute当前class)
---------------------------------------------------------------------------------------------------------------------------------------------------------
对象与类的总结:(注意事项,ruby会打开已定义的类和模块所以在定义新类名和模块名时切忌避免冲突和混乱)
什么是对象?
对象就是指向类的实例变量,在ruby里面一切皆是对象除了:block,method
什么是类?
类即是Class的实例对象,加上一列实例方法和指向一个superclass
classes,Object,BasicObject,Module...的类都是Class,Class类的类是自己
Class是Molule的子类,所以a class is also a module.
---------------------------------------------------------------------------------------------------------------------------------------------------------
当你include一个module到class或module时ruby会创建一个匿名类包含那个module的内容,
then inserts the anonymous class in the chain,just above the including class itself.
---------------------------------------------------------------------------------------------------------------------------------------------------------
self # => main
self.class # => Object
在ruby的顶层空间是main对象(Object的一个实例),他提供顶层执行环境——>意思时ruby所有操作都是在对象内部执行的。
#补充介绍Kernel
Ruby includes some methods, such as print( ), that you can call from
anywhere in your code. they are actually private instance methods of module Kernel.
class Object includes Kernel, so Kernel gets into every object’s ancestors chain
---------------------------------------------------------------------------------------------------------------------------------------------------------
#令人混淆的self
当前对象-->self:可以理为一个全局变量,在任意时刻只代表一个对象,只有在明确指定方法调用接收者时self才被赋值为该对象.
两种特殊情况:
1.当self的值是main对象时出现class或module定义时self的值会被赋值为定义的class或module对象。
2.在对象A内调用其他对象的方法并返回到A对象后self还是为A对象.
#example:
class C
def initialize(object)
#这里的self是c即C的例
object.methods.grep(/methods$/){
#这里的self仍然是c因为object在调用methods后返回数据,又回到了c的环境中故self此时表示c
C.cla_method
}
end
def self.cla_method
end
end
c = C.new(String)
---------------------------------------------------------------------------------------------------------------------------------------------------------
#动态方法的应用实例:
class Computer
def initialize(computer_id, data_source)
@id = computer_id
@data_source = data_source
data_source.methods.grep(/^get_(.*)_info$/) { Computer.define_component $1 }
end
def self.define_component(name)
define_method(name) {
info = @data_source.send "get_#{name}_info" , @id
price = @data_source.send "get_#{name}_price" , @id
result = "#{name.capitalize}: #{info} ($#{price})"
return "* #{result}" if price >= 100
result
}
end
end
#优点:当data_source增加电脑组件时函数能自动识别并为起定义访问器.
---------------------------------------------------------------------------------------------------------------------------------------------------------
#用method_missing作代理:
class Computer
#to make Computer a blankslate,目的是避免使用method_missing方法代理时与已有方法冲突
#also can directly inherited from BasicObject to be a blankslate.
instance_methods.each do |m|
undef_method m unless m.to_s =~ /^__|method_missing|respond_to?/
end
def initialize(computer_id, data_source)
@id = computer_id
@data_source = data_source
end
def method_missing(name, *args)
super if !@data_source.respond_to?("get_#{name}_info" )
info = @data_source.send("get_#{name}_info" , args[0])
price = @data_source.send("get_#{name}_price" , args[0])
result = "#{name.to_s.capitalize}: #{info} ($#{price})"
return "* #{result}" if price >= 100
result
end
#使respond_to?方法正确显示代理是否能接收某个方法。
def respond_to?(method)
@data_source.respond_to?("get_#{method}_info" ) || super
end
end
#该动态代理缺点在于速度慢,可以通过第一次动态创建方法,下一次直接调用定义的方法进行优化(动态定义方法与动态代理的组合)
#example:
def method_missing(name,*args)
super if !@object.respond_to?(name)
define_mehtod(name)
puts "create method #{name} successfully!"
end
end
---------------------------------------------------------------------------------------------------------------------------------------------------------
局部变量的生命周期:进入其范现生成,离开其范围自动消失。
---------------------------------------------------------------------------------------------------------------------------------------------------------
The code in a class or module definition is executed
immediately. Conversely, the code in a method definition is executed
later, when you eventually call the method.
---------------------------------------------------------------------------------------------------------------------------------------------------------
Scope Wrap-Up
#ruby的scope gates:class,module,def
#Flat Scope:(让变量穿越不同的gate)
my_var = "Success"
MyClass = Class.new do
puts "#{my_var} in the class definition!"
define_method :my_method do
puts "#{my_var} in the method!"
end
end
MyClass.new.my_method
#output:
⇒ Success in the class definition!
Success in the method!
#Shared Scope:(在同一个gate内共享变量)
def define_methods
shared = 0
Kernel.send :define_method, :counter do
shared
end
Kernel.send :define_method, :inc do |x|
shared += x
end
end
define_methods
counter # => 0
inc(4)
counter # => 4
#block的变量范围(ruby1.9以后)
def my_method
yield(2)
end
x = 1
y = 1
my_method do |x;y|
#这里的y是声明的块级变量,默认值为nil
puts "inner block x:#{x}, y:#{y}"
end
puts "outer block x:#{x}, y:#{y}"
#inner block x:2, y:
#outer block x:1, y:1
---------------------------------------------------------------------------------------------------------------------------------------------------------
instance_exec( ) is similar to instance_eval( ), but it also allows you to pass arguments to the block.
---------------------------------------------------------------------------------------------------------------------------------------------------------
#Clean Rooms
提供一个干净区域来执行代码块,主要用于减少冲突
#example:
class CleanRoom
def complex_calculation
# ...
end
def do_something
# ...
end
end
clean_room = CleanRoom.new
clean_room.instance_eval do
if complex_calculation > 10
do_something
end
end
---------------------------------------------------------------------------------------------------------------------------------------------------------
package code:
• In a proc, which is basically a block turned object
• In a lambda, which is a slight variation on a proc
• In a method
#notice: proc and lambda are Proc,Proc is a object,but blocks not.
&res变量放在方法参数最后用以接收代码块,此时res就代表proc
yield 只能回调代码块。
----------------------------------------------------------------------------------------------------------------------------------------------------
block,proc,lambda,mehtod object间的差异:
1.method object 几乎与lambda相同只有一点:lambda执行的范围是定义它时的范围(闭包),Method object是在他所在实例的范围内执行。
#method object的例子:
#thod_object = Myclass.new.method :my_method
#method_object.call
2.
---------------------------------------------------------------------------------------------------------------------------------------------------------
keep in mind that a class is just a souped-up module, so anything you
learn about classes also applies to modules.
---------------------------------------------------------------------------------------------------------------------------------------------------------
#***********Import**********
#instance_eval() changes self, and class_eval() changes both self and the current class
#关于instance_eval() 与 class_eval()
1.class_eval顾名思义就是在类环境中执行代码,相当于reopen the class(注:module_eval是其别名,他们只能对class或module使用)
instance_eval就是在对象中执行代码(注意classes也是对象)
2.若对象使用instance_eval定义方法,那么方法是该对象的singleton_method(若该对象是类那么定义的方法为class_method)
若类使用class_eval定义方法,那么方法是类的instance_method
---------------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------------
#关于instance_variable和class_instancece_variable
1.实例变量被存储在实例对象里,只在该对象内共享,不能被该对象的实例或子类使用
2.类也是对象,所以类实例变量只能在类对象内共享
#example:
class MyClass
@my_var = 1 #class_instance_variable
def self.read; @my_var; end
def write; @my_var = 2; end #instance_variable
def read; @my_var; end
end
obj = MyClass.new
obj.write
obj.read # => 2
MyClass.read # => 1
---------------------------------------------------------------------------------------------------------------------------------------------------------
#@@v被定义在main对象的环境中,main是Object类的实例,然而MyClass是Ojcect的子类
#所以MyClass继承了class_variable @vv,故最后结果是2
@@v = 1
class MyClass
@@v = 2
end
@@v # => 2
---------------------------------------------------------------------------------------------------------------------------------------------------------
Class#new( ) also accepts an argument (the superclass of thenew class)
and a block that is evaluated in the context of the newborn class:
#example:
c = Class.new(Array) do
def my_method
'Hello!'
end
end
---------------------------------------------------------------------------------------------------------------------------------------------------------
类方法就是类的singleton方法
#def obj.a_singleton_method; end
#def MyClass.another_class_method; end
---------------------------------------------------------------------------------------------------------------------------------------------------------
#当一个类在被其他类调用时怎么进行重构?
class Book
def title # ...
def subtitle # ...
def lend_to(user)
puts "Lending to #{user}"
# ...
def self.deprecate(old_method, new_method)
define_method(old_method) do |*args, &block|
warn "Warning: #{old_method}() is deprecated. Use #{new_method}()."
send(new_method, *args, &block)
end
end
deprecate :GetTitle, :title
deprecate :LEND_TO_USER, :lend_to
deprecate :title2, :subtitle
end
#b = Book.new
#b.LEND_TO_USER("Bill" )
#⇒ Warning: LEND_TO_USER() is deprecated. Use lend_to().
# Lending to Bill
---------------------------------------------------------------------------------------------------------------------------------------------------------
#eigenclass只有一个实例且不能被继承,对象的singleton方法就放在eigenclass里面。
#singleton method
def obj.my_singleton_method; end
eigenclass.instance_methods.grep(/my_/) # => ["my_singleton_method"]
#class mehtod
class MyClass
def self.my_method; end
end
def MyClass.my_other_method; end
class MyClass
class << self
def my_method; end
end
end
分享到:
相关推荐
Ruby是一种高级、面向对象的编程语言,由日本的松本行弘(Yukihiro Matsumoto)于1995年设计并开发,直至2006年已经成熟并广泛使用。Ruby语言以其简洁、优雅的语法和强大的元编程能力而闻名,它注重程序员的生产力,...
这个“ruby基础学习资料”压缩包很显然是为那些想要入门或者深入理解Ruby的人准备的。让我们详细探讨一下Ruby的基础知识。 1. **变量与数据类型**: Ruby支持五种基本的数据类型:符号(Symbol)、字符串(String...
“基于闭包的JavaScript面向对象编程框架” 本文总结了基于闭包的JavaScript面向对象编程框架的设计和实现。通过使用闭包,实现了基于类的面向对象编程的封装、继承和多态特征。 闭包(Closure)是JavaScript中的...
面向对象编程(面向对象编程)是Python的核心特性之一,笔记中讲解了类的创建、属性、方法、继承、封装、多态以及类的高级用法,如类变量、静态方法、类方法、__slots__等。函数重写和多态的介绍,使初学者能够理解...
这个“Ruby基础语法使用方法全套讲解,风格讲解指南大全”将帮助初学者全面了解Ruby语言,从简单的数据操作到复杂的面向对象编程,一步步掌握Ruby的魅力。通过深入学习和实践,你将能够编写出高效、优雅的Ruby代码。
面向对象编程(OOP)和闭包详解 面向对象编程(OOP)是一种编程范式,它将程序视为一组对象的交互。闭包是 JavaScript 中一个重要的概念,指的是一个函数可以使用函数之外定义的变量。下面我们将详细介绍面向对象...
Ruby是一种面向对象的编程语言,以其简洁的语法和强大的功能深受开发者喜爱。在Ruby的世界里,函数式编程的概念也被巧妙地融入其中,这体现在“Functo”这样的库上。标题提到的“Ruby-Functo”就是Ruby中实现可组合...
8. **闭包和块**:Ruby中的块和闭包是重要的概念,它们允许函数或方法捕获和操作外部变量,是实现函数式编程风格的关键。 9. **正则表达式**:Ruby内置了强大的正则表达式引擎,提供了丰富的匹配和替换功能。 10. ...
在Ruby笔记3中,我们将会深入探讨这个强大的语言的各个方面,包括基础语法、类与对象、模块、方法、控制结构、异常处理、文件操作以及一些高级特性。 首先,让我们从基础语法开始。Ruby中的变量分为四种类型:局部...
Ruby是一种高级、面向对象的脚本编程语言,由日本的松本行弘(Yukihiro Matsumoto)在1995年设计并创建。它以其简洁、优雅的语法和强大的编程模型而闻名,旨在提高程序员的生产力和代码可读性。本教程将带你深入学习...
"ruby笔记2ruby笔记2ruby笔记2"可能是指一系列关于Ruby学习的笔记,这些笔记可能涵盖了Ruby的基础概念、核心特性以及进阶话题。在Ruby的学习过程中,理解和掌握以下几个关键知识点至关重要: 1. **面向对象编程...
Ruby是一种面向对象的、动态类型的编程语言,以其简洁和优雅的语法著称。在这个"Ruby基础"的主题中,我们将深入探讨Ruby语言的核心概念和特性,以及如何利用它进行高效的编程。 首先,我们要理解Ruby的基本语法。...
个人在学习前端JavaScript时的学习笔记,内含JavaScript的中基础知识点,以及案例。里面有兼容市面上浏览器的方法 1.知识点包括:基础语法,字符串操作,object对象,DOM对象,BOM对象,offset,client,scroll对象...
学习Ruby语言,不仅可以提升编程技能,还能了解面向对象编程的思想,为未来学习其他编程语言或深入理解软件工程原理打下坚实基础。对于那些寻求高效、简洁且具有强大表达力的编程工具的人来说,Ruby是一个值得探索的...
闭包(closure)是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现
Ruby是"一种用于迅速和简便的面向对象编程的解释性脚本语言";这意味着什么? 解释性脚本语言: 有直接呼叫系统调用的能力 强大的字符串操作和正则表达式 开发中快速回馈 迅速和简便: 无需变量声明 变量无...
Ruby是一种面向对象的、动态类型的编程语言,由日本的松本行弘(Yukihiro Matsumoto)于1995年设计并开发。它强调代码的简洁性和可读性,致力于提供一种更自然的编程方式。Ruby的学习资源丰富多样,包括书籍、在线教程...
JavaScript 基础学习笔记 本资源摘要信息基于黑马视频记录的学习笔记,涵盖了 JavaScript 基础知识点,包括 HTML、CSS、JavaScript、DOM、事件处理等内容。 HTML 和 CSS 基础 * HTML 结构:head、body、title、...
本文将深入探讨Python的基础知识,包括变量、数据类型、运算符、流程控制语句、字符串、元组、列表、字典、集合以及函数和闭包的概念。 ### 变量与数据类型 在Python中,变量是用来存储值的容器,可以随时赋值和...