1.
When puts
needs to convert an
object to a string, it calls that object’s to_s
method.
2. I
nheritance allows you to create a
class that is a refinement or specialization of another class. This class is
called a subclass of the original, and the original is a superclass of the
subclass. The child inherits all of the capabilities of its parent class—all
the parent’s instance methods are available in instances of the child:
class Child < Parent
end
3.
The superclass
method returns
the parent of a particular class.
4. I
f you don’t define an explicit superclass
when defining a class, Ruby automatically makes the built-in class Object
that class’s parent.
5.
class BasicObject
was
introduced in Ruby 1.9 as the parent of Object
. It is used in certain kinds
of metaprogramming acting as a blank canvas. BasicObject
is the root
class of the hierarchy of classes.
6.
to_s
is actually
defined in class Object
.
7. R
uby comes with a library called GServer
that implements basic TCP server functionality. The GServer
class handles all
the mechanics of interfacing to TCP sockets. When you create a GServer
object, you tell it the port to listen on. When a client connects, the GServer
object calls its serve
method to handle that connection.
8.
When you invoke super
, Ruby sends a
message to the parent of the current object, asking it to invoke a method of
the same name as the method invoking super
. It passes this method the
parameters that were passed to super
.
9.
A Ruby class has only one direct
parent. However, Ruby classes can include the functionality of any number of mixins
(a mixin is like a partial class definition).
10.
Modules are a way of grouping together
methods, classes, and constants. Modules give you two major benefits:
a)
Modules provide a namespace and
prevent name clashes.
b)
Modules support the mixin facility.
11.
Modules define a namespace, a sandbox
in which your methods and constants can play without having to worry about
being stepped on by other methods and constants.
12.
Module constants are named just like
class constants, with an initial uppercase letter. module methods are defined
just like class methods. You call a module method by preceding its name with
the module’s name and a period, and you reference a constant using the module
name and two colons:
#trig.rb
module Trig
PI = 3.141592654
def Trig.sin(x)
# ..
end
def Trig.cos(x)
# ..
end
end
#moral.rb
module Moral
VERY_BAD = 0
BAD = 1
def Moral.sin(badness)
# ...
end
end
#pinhead.rb
require_relative 'trig'
require_relative 'moral'
y = Trig.sin(Trig::PI/4)
wrongdoing = Moral.sin(Moral::VERY_BAD)
13.
A module can’t have instances, because
a module isn’t a class. However, you can include a module within a class
definition. When this happens, all the module’s instance methods are suddenly
available as methods in the class as well. They get mixed in. In fact, mixed-in
modules effectively behave as superclasses:
module Debug
def who_am_i?
"#{self.class.name} (id: #{self.object_id}): #{self.name}"
end
end
class Phonograph
include Debug
attr_reader :name
def initialize(name)
@name = name
end
# ...
end
class EightTrack
include Debug
attr_reader :name
def initialize(name)
@name = name
end
# ...
end
ph = Phonograph.new("West End Blues")
et = EightTrack.new("Surrealistic Pillow")
ph.who_am_i? # => "Phonograph (id: 2151894340): West End Blues"
et.who_am_i? # => "EightTrack (id: 2151894300): Surrealistic Pillow"
14.
A Ruby include
does not simply
copy the module’s instance methods into the class. Instead, it makes a reference
from the class to the included module. If multiple classes include that module,
they’ll all point to the same thing. If you change the definition of a method
within a module, even while your program is running, all classes that include
that module will exhibit the new behavior. Instance variables are always per
object.
15.
The Comparable
mixin adds the
comparison operators (<
, <=
, ==
,
>=
,
and >
),
as well as the method between?
, to a class. For this to
work, Comparable
assumes that any class that uses it defines the
operator <=>
. So, as a class writer, you define one method, <=>
;
include Comparable
; and get six comparison functions for free.
16.
If you write an iterator called each
,
which returns the elements of your collection in turn.Mix in Enumerable
,
and suddenly your class supports things such as map
, include?
, and find_all?
.
If the objects in your collection implement meaningful ordering semantics using
the <=>
method, you’ll also get methods such as min, max, and sort.
17.
Because inject
is made available
by Enumerable
,
we can use it in any class that includes the Enumerable
module and
defines the method each
:
class VowelFinder
include Enumerable
def initialize(string)
@string = string
end
def each
@string.scan(/[aeiou]/) do |vowel|
yield vowel
end
end
end
vf = VowelFinder.new("the quick brown fox jumped")
vf.inject(:+) # => "euiooue"
18.
The module you mix into your client
class (the mixee?
) may create instance variables in the client object and
may use attr_reader
and friends
to define accessors for
these instance variables. For instance, the Observable
module in the
following example adds an instance variable @observer_list
to any
class that includes it:
module Observable
def observers
@observer_list = []
end
def add_observer(obj)
observers << obj
end
def notify_observers
observers.each {|o| o.update }
end
end
19.
A mixin’s instance variables can clash
with those of the host class or with those of other mixins. For the most part,
mixin modules don’t use instance variables directly—they use accessors to
retrieve data from the client object. But if you need to create a mixin that
has to have its own state, ensure that the instance variables have unique names
to distinguish them from any other mixins in the system (perhaps by using the
module’s name as part of the variable name). Alternatively, the module could
use a module-level hash, indexed by the current object ID, to store
instance-specific data without using Ruby instance variables:
module Test
State = {}
def state=(value)
State[object_id] = value
end
def state
State[object_id]
end
end
class Client
include Test
end
c1 = Client.new
c2 = Client.new
c1.state = 'cat'
c2.state = 'dog'
c1.state # => "cat"
c2.state # => "dog"
A downside of this approach is that the data associated
with a particular object will not get automatically deleted if the object is
deleted. In general, a mixin that requires its own state is not a mixin—it
should be written as a class.
20.
Ruby looks first in the immediate
class of an object, then in the mixins included into that class, and then in
superclasses and their mixins. If a class has multiple modules mixed in, the last
one included is searched first.
When
you’re looking for subclassing relationships while designing your application,
be on the lookout for the is-a relationships. We need to be using composition wherever
we see a case of A uses a B, or A has a B. In this case mixin can help.
分享到:
相关推荐
New, Improved, and Deprecated Modules email elementtree functools itertools collections threading datetime and time math abc io reprlib logging csv contextlib decimal and fractions ftp ...
嵌入式八股文面试题库资料知识宝典-华为的面试试题.zip
训练导控系统设计.pdf
嵌入式八股文面试题库资料知识宝典-网络编程.zip
人脸转正GAN模型的高效压缩.pdf
少儿编程scratch项目源代码文件案例素材-几何冲刺 转瞬即逝.zip
少儿编程scratch项目源代码文件案例素材-鸡蛋.zip
嵌入式系统_USB设备枚举与HID通信_CH559单片机USB主机键盘鼠标复合设备控制_基于CH559单片机的USB主机模式设备枚举与键盘鼠标数据收发系统支持复合设备识别与HID
嵌入式八股文面试题库资料知识宝典-linux常见面试题.zip
面向智慧工地的压力机在线数据的预警应用开发.pdf
基于Unity3D的鱼类运动行为可视化研究.pdf
少儿编程scratch项目源代码文件案例素材-霍格沃茨魔法学校.zip
少儿编程scratch项目源代码文件案例素材-金币冲刺.zip
内容概要:本文深入探讨了HarmonyOS编译构建子系统的作用及其技术细节。作为鸿蒙操作系统背后的关键技术之一,编译构建子系统通过GN和Ninja工具实现了高效的源代码到机器代码的转换,确保了系统的稳定性和性能优化。该系统不仅支持多系统版本构建、芯片厂商定制,还具备强大的调试与维护能力。其高效编译速度、灵活性和可扩展性使其在华为设备和其他智能终端中发挥了重要作用。文章还比较了HarmonyOS编译构建子系统与安卓和iOS编译系统的异同,并展望了其未来的发展趋势和技术演进方向。; 适合人群:对操作系统底层技术感兴趣的开发者、工程师和技术爱好者。; 使用场景及目标:①了解HarmonyOS编译构建子系统的基本概念和工作原理;②掌握其在不同设备上的应用和优化策略;③对比HarmonyOS与安卓、iOS编译系统的差异;④探索其未来发展方向和技术演进路径。; 其他说明:本文详细介绍了HarmonyOS编译构建子系统的架构设计、核心功能和实际应用案例,强调了其在万物互联时代的重要性和潜力。阅读时建议重点关注编译构建子系统的独特优势及其对鸿蒙生态系统的深远影响。
嵌入式八股文面试题库资料知识宝典-奇虎360 2015校园招聘C++研发工程师笔试题.zip
嵌入式八股文面试题库资料知识宝典-腾讯2014校园招聘C语言笔试题(附答案).zip
双种群变异策略改进RWCE算法优化换热网络.pdf
内容概要:本文详细介绍了基于瞬时无功功率理论的三电平有源电力滤波器(APF)仿真研究。主要内容涵盖并联型APF的工作原理、三相三电平NPC结构、谐波检测方法(ipiq)、双闭环控制策略(电压外环+电流内环PI控制)以及SVPWM矢量调制技术。仿真结果显示,在APF投入前后,电网电流THD从21.9%降至3.77%,显著提高了电能质量。 适用人群:从事电力系统研究、电力电子技术开发的专业人士,尤其是对有源电力滤波器及其仿真感兴趣的工程师和技术人员。 使用场景及目标:适用于需要解决电力系统中谐波污染和无功补偿问题的研究项目。目标是通过仿真验证APF的有效性和可行性,优化电力系统的电能质量。 其他说明:文中提到的仿真模型涉及多个关键模块,如三相交流电压模块、非线性负载、信号采集模块、LC滤波器模块等,这些模块的设计和协同工作对于实现良好的谐波抑制和无功补偿至关重要。
内容概要:本文探讨了在工业自动化和物联网交汇背景下,构建OPC DA转MQTT网关软件的需求及其具体实现方法。文中详细介绍了如何利用Python编程语言及相关库(如OpenOPC用于读取OPC DA数据,paho-mqtt用于MQTT消息传递),完成从OPC DA数据解析、格式转换到最终通过MQTT协议发布数据的关键步骤。此外,还讨论了针对不良网络环境下数据传输优化措施以及后续测试验证过程。 适合人群:从事工业自动化系统集成、物联网项目开发的技术人员,特别是那些希望提升跨协议数据交换能力的专业人士。 使用场景及目标:适用于需要在不同通信协议间建立高效稳定的数据通道的应用场合,比如制造业生产线监控、远程设备管理等。主要目的是克服传统有线网络限制,实现在不稳定无线网络条件下仍能保持良好性能的数据传输。 其他说明:文中提供了具体的代码片段帮助理解整个流程,并强调了实际部署过程中可能遇到的问题及解决方案。
基于C#实现的检测小说章节的重复、缺失、广告等功能+源码+项目文档,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用,详情见md文档 基于C#实现的检测小说章节的重复、缺失、广告等功能+源码+项目文档,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用,详情见md文档~ 基于C#实现的检测小说章节的重复、缺失、广告等功能+源码+项目文档,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用,详情见md文档 基于C#实现的检测小说章节的重复、缺失、广告等功能+源码+项目文档,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用,详情见md文档 基于C#实现的检测小说章节的重复、缺失、广告等功能+源码+项目文档,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用,详情见md文档 基于C#实现的检测小说章节的重复、缺失、广告等功能+源码+项目文档,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用,详情见md文档