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

前情提要:面试

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

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

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

足迹面试问题个人总结
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(类别方法)。lua

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

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

若是都找不到Method的话呢?图片

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

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!!!!:)

相关文章
相关标签/搜索