Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Ruby

เชี่ยวชาญ Ruby `method` สำหรับ Debugging ขั้นสูงและ Dependency Management

เคยอยากส่ง method ให้ function ที่รับแค่ block? หรือหาว่า superclass ไหนของ object พัง method ที่พยายามเรียก?

สิ่งเหล่านี้ทำง่ายด้วย method method ใช้ดี เรียนรู้ dependencies, ประหยัดชั่วโมง debugging, ส่งโค้ดไปที่ที่ต้องการ

Methods ง่ายเหมือน Lambdas

Ruby methods มาก รับ blocks หรือ lambdas แตส่ง method ให้ method อื่นโดยตรง ไม่ได้แบบ lambda ต้องใช้ method ก่อน:

irb(main):001:0> sin_method = Math.method(:sin) => #<Method: Math.sin> irb(main):002:0> (1..10).map(&sin_method) => [0.8414709848078965, 0.9092974268256817, 0.1411200080598672, -0.7568024953079282, -0.9589242746631385, -0.27941549819892586, 0.6569865987187891, 0.9893582466233818, 0.4121184852417566, -0.5440211108893699] 

เกิดอะไรขึ้น?

บรรทัดแรก เปลี่ยน method Math.sin เป็น Method object Just like lambdas, `Method` objects respond to `call` Method objects respond to to_proc (Thanks, Benoit) ใช้ในที่เดียวกับ lambda ได้:

irb(main):004:0> sin_method = -> (x) { Math.sin(x) } => #<Proc:0x007fe9f90a9bd8@(irb):4 (lambda)> irb(main):005:0> (1..10).map(&sin_method) => [0.8414709848078965, 0.9092974268256817, 0.1411200080598672, -0.7568024953079282, -0.9589242746631385, -0.27941549819892586, 0.6569865987187891, 0.9893582466233818, 0.4121184852417566, -0.5440211108893699] 

ดูบรรทัดแรกอีกครั้ง โค้ด lambda นี้ ทำงานเหมือน code ก่อนหน้าที่ใช้ method

ขึ้นอยู่กับการเขียนโค้ด การได้ Method object อาจง่ายกว่า wrap ใน lambda ตอนต้องการ lambda แตมีแค่ method จำ method method ไว้

เมธอดนั้นมาจากไหน?

เรียก method บน object แตทำอย่างไม่คาดคิด อาจมีคน override หรือ monkey-patch แล้วหาได้ยังไง?

อาจ ขุด source code ชั่วโมงๆ แต Method มี 2 methods ช่วยเร็วขึ้น

หา class ที่ define method ที่เรียก ใช้ owner:

irb(main):003:0> Task.new.method(:valid?).owner => ActiveRecord::Validations 

ตอนอยากลึกกว่า หาว่า method ถูก define ที่ไหน ใช้ source_location:

irb(main):004:0> Task.new.method(:valid?).source_location => ["/usr/local/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta2/lib/active_record/validations.rb", 55] 

source_location คืน array element แรก path ไฟล์ที่ define method element ที่สอง line number ด้วยอันนี้ รู้พอดีว่าต้องดูไหนต่อ

อ่าน Source โดยไม่ต้องขุด

ตอนรู้ ที่ไหน define method อาจอยากดู อย่างไร define

Method ทำเองไม่ได้ แตติดตั้ง method_source gem จะดู source code หลาย methods ได้จาก console:

irb(main):002:0> puts Task.new.method(:valid?).source def valid?(context = nil) context ||= (new_record? ? :create : :update) output = super(context) errors.empty? && output end => nil 

ยังดู comments ได้:

irb(main):003:0> puts Task.new.method(:valid?).comment # Runs all the validations within the specified context. Returns +true+ if # no errors are found, +false+ otherwise. # # Aliased as validate. # # If the argument is +false+ (default is +nil+), the context is set to <tt>:create</tt> if # <tt>new_record?</tt> is +true+, and to <tt>:update</tt> if it is not. # # Validations with no <tt>:on</tt> option will run no matter the context. Validations with # some <tt>:on</tt> option will only run in the specified context. => nil 

เก๋มากไหม? Documentation อยู่ใน console เลย!

Introspection เก๋มาก

Ruby classes โปรดของผมคือ class ที่ inspect โค้ดจากในโค้ดได้ Classes อย่าง Class, Module, Method ด้วยอันเหล่านี้ เรียนรู้เยอะเกี่ยวโค้ดขณะรัน และ modify on the fly เรียนรู้ classes นี้ดี เรียน API และทำสิ่งเก๋มากกับ Ruby ได้