Day10 - Ruby如何调用方法(invoke method)?

前情提要:

不知不觉到了第10天罗!。进度1/3(挺胸!xcsjbj)接下来应该会进入学习的深水区,但是我会越战越勇。路遥知马力,日久见人心!?‍♀️

Ruby经典面试题目#10
Ruby如何引入方法?Please explain how Ruby looks up a method to invoke?

每当不知从何下笔时,起手式就是开始回顾之前的文章,盘点我已经走了多远、以及基础观念到底掌握了多少:

足迹面试问题我的总结
Day 1 class class建立物件实体,以method和数据互动
Day 2 class与module class可继承,module不可继承
Day 3 module module里的method可被include和extend
Day 4 instance method与class method include用于instance method,extend用于class method
Day 5 self物件与singleton method singleton method是singleton class的instence method
Day 6 public,protected,private method在classs外无法取得protected或private method
Day 7 symbol与string:符号symbol class的物件实体,object_id相同/字串:string变数指向字串物件,object_id不同
Day 8 concat与+= method以concat串接,object_id相同/ += object_id不同
Day 9 ||= method(or-equals)条件判断a||=b是a || a = b缩写,意思为条件表达式a?a:a = b
洋洋洒洒地条列出这么多方法之后,我们好奇的问,

Ruby到底是怎么寻找这些方法的呢?
Ruby最先寻找的地方是物件的eigenclass(特征类别,物件上层的隐藏类别)method会直接定义在里面,如同Day 5提到的singleton method(类别方法)。

如果Ruby没有办法在物件的eigenclass找到,它会寻找此物件class所属的上一层(ancestor)class、层层往上搜寻,深入到Object、到Kernal、最后去BasicObject搜寻method是否在里面。

https://s3-ap-southeast-2.amazonaws.com/tingsrailsdemo/class.png图片来源

如果都找不到Method的话呢?

不用担心,Ruby就像Google Map一样给予提示,它会在内部搜寻另一个:method_missing method给这个物件的class,提供Ruby工程师解bug的线索:

undefined method `某方法名称'(NoMethodError)
这个线索我们并不陌生,因为我们已经有多次经验了:

在Day 6,无法使用class里的.protected及.private方法

day6.protected #=> undefined method `protected'(NoMethodError)
day6.private #=> undefined method `private'(NoMethodError)
在Day 7Symbol找不到[]=方法

:tingsmessage[1]=“Z”
#undefined method `[]=' for:tingsmessage:Symbol(NoMethodError)
我从https://ruby-doc.org/core-2.5.1/列出我在前十篇文章所用到的实体方法(Public Instance Methods),整理表格如下:

Object Kernal BasicObject
class→class puts(obj,…)→nil object_id→integer
extend(module)→obj String(arg)→string send(symbol [,args…])→obj
singleton_method(sym)→method Hash(arg)→hash new()这个是Public Class Methods!
为了更清楚厘清自己的观念,以及未来寻找海外工作、面试语言的需要,我决定用英文整理出这10天的学习纪录,并附上手册连接:

观念解释
class Classes in Ruby are first-class objects.
module Modules serve two purposes in Ruby,namespacing and mix-in functionality.
class method Class methods(methods on a module)may be called directly.
instance method Instance methods defined in a module are only callable when included.
include when the module is included,istance methods appear as methods in a class.(module methods do not.)
extend Adds to obj the instance methods from each module given as a parameter.
self Self refers to the object that defines the current scope.(it will change when it a different method or a new module).
singleton class Returns an array of the names of singleton methods for obj.(If object is nil,true,or false,it returns NilClass,TrueClass,or FalseClass.)
singleton method The behavior of an instance is determined by its class.
public method With no arguments,sets the default visibility for subsequently defined methods to public.With arguments,sets the named methods to have public visibility.
projected method If a method has protected visibility,it is callable only where self of the context is the same as the method.
private method With no arguments,sets the default visibility for subsequently defined methods to private.With arguments,sets the named methods to have private visibility.
symbol Symbol objects represent names and some strings inside the Ruby interpreter.
string A String object holds and manipulates an arbitrary sequence of bytes
concat(string method)Concatenates the given object(s)to str. If an object is an Integer,it is converted to a character before concatenation.
+(string method)Concatenation—Returns a new String containing
true|(TrueClass method)Or—Returns true.As obj is an argument to a method call,it is always evaluated
感想(chao-ok-huangdaoyi):

为了做Ruby如何invoke method的表格,我竟然把Ruby API的Object,Kernal,BasicObject页面看了好几次,了解输入的参数怎么用、已经输出的物件会是什么形式。

这是我在参与之前没想过自己会做的事(感觉里里外外地翻阅手册是挺高的境界啊!)

也了解到,经典面试题为何成为经典的原因,是因为它们实实在在地概括到Ruby基本最重要的概念。

经过这10天成长收获巨大!明天来继续研究更多method!!!!:)

猜你喜欢

转载自www.cnblogs.com/lannyQ-Q/p/10801701.html