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

相關文章
相關標籤/搜索