Những kiến thức thường gặp dành cho các Ruby Developer mới (Part 1)
Bài đăng này đã không được cập nhật trong 8 năm
Giới thiệu
Ruby là một ngôn ngữ lập trình rất phổ biển, nhất với các Ruby on Rails developer. Đối với những người mới bắt đầu, chắc sẽ khá nhiều vấn đề và sự nhầm lẫn trong khi lập trình Ruby. Sau đây, mình sẽ giới thiệu các kiến thức quan trọng và phổ biến dành cho các Ruby developer ai cũng phải biết.
Instance Variables
Đối với các ngôn ngữ lập trình khác, instance viarables phải được khái báo trước khi nó được assign giá trị. Nhưng trong Ruby, instance variable không cần phải khái báo, nó sẽ tồn tại trong khi nó đã được assign giá trị đầu tiên.
Để tạo instance variable trong Ruby là chỉ cần thêm @ ở trước các local variables.
class Item
  def initialize(title)
    @title = title // instance variable @title
  end
end
Để có thể truy cập đến các instance variable từ bên ngoài, phải sử dụng getter và setter method.
class Item
  def title=(t)
    @title = t
  end
  def title
    @title      
  end
end
item = Item.new
puts item.title.inspect # => nil
item.title = "Chia Ruby"
puts item.title # => "Chia Ruby"
Ví dụ trên constructor initialize được xoá đi để chứng minh rằng hàm này là optional.
Sử dụng getters và setters càng nhiều và lặp đi lặp lại nhiều lẫn sẽ làm cho code rất dài và không đẹp. Để tránh việc lặp, Ruby cung cấp 3 helper methods sau đây:
- #attr_reader– define instance-level getters
- #attr_writer– define instance-level setters
- #attr_accessor– define both Thông thường, các helper method trên thường để ở trên cùng.
class Thing
  attr_accessor :foo, :bar
  attr_reader :baz
  def initialize
    @baz = "cat"
  end
end
thing = Thing.new
thing.foo = 1
thing.bar = 2
puts thing.baz # => "cat"
puts thing.foo # => 1
puts thing.bar # => 2
Modules
Trong Ruby,  module là container của các methods. Còn class là một loại module đặc biệt có khả năng tạo ra các instances và có ancestors (tổ tiên).
module MyModule
  def hello
    puts "hello from instance"
  end
end
class MyClass
  def hello
    puts "hello from instance"
  end
end
instance = MyClass.new
instance.hello # => "hello from instance"
Methods ở trong module có thể là instance hoặc class methods.
module MyModule
  def self.hello # hoặc def MyModule.hello
    puts 'hello from module'  
  end
end
MyModule.hello # => "hello from module"
module không thể tạo các instances nhưng module có thể có instance variables như trong class.
module Fooable
  def self.foo=(value)
    @foo = value
  end
  def self.foo
    @foo
  end
end
Fooable.foo = "baz"
puts Fooable.foo # => "baz"
Các methods hoặc instance variable trong module sẽ được gọi ở trong các class khác với mục đích tài sử dụng code.
Để dùng được ở trong class khác, chúng ta sẽ sử dụng mixins.
Mixins
Có 2 cách để mix module vào trong class.
- #include– chuyển methods của module thành instance methods.
- #extend– chuyển methods của module thành class methods.
module Helloable
  def hello
    puts "Hello World"
  end
end
class IncludeClass
  include Helloable
end
class ExtendClass
  extend Helloable
end
IncludeClass.new.hello
ExtendClass.hello
Module Gotchas
- Nếu 2 modules define method giống nhau, module thứ 2 include vào sẽ được dùng.
module Module1
  def hello
    "hello from module 1"
  end
end
module Module2
  def hello
    "hello from module 2"
  end
end
class HelloClass
  include Module1
  include Module2
end
HelloClass.new.hello # => "hello from module 2"
- Nếu một module được include 2 lần, inclusion thứ 2 sẽ được bỏ qua.
module HelloModule
  def say
    "hello from module"
  end
end
module GoodbyeModule
  def say
    "goodbye from module"
  end
end
class MyClass
  include HelloModule
  include GoodbyeModule
  include HelloModule
end
MyClass.new.say # => "goodbye from module"
- Module methods không thể thay thế các methods đã defined ở trong một class.
module HelloModule
  def hello
    'hello from module'
  end
end
class HelloClass
  def hello
    'hello from class'
  end
  include HelloModule
end
HelloClass.new.hello # => 'hello from class'
Conclusion
Mình xin kết thúc phần 1 ở đây. Phần này chứa khá nhiều kiến thức cơ bản và cần thiết mà cac Ruby developer phải biết và hiểu rõ về chúng. Chi tiết hơn, bạn có thể tham khoa các tài liệu sau đây: https://www.sitepoint.com/common-trip-ups-new-rubyists-part/ https://matt.aimonetti.net/posts/2012/07/30/ruby-class-module-mixins/ https://www.vikingcodeschool.com/professional-development-with-ruby/classes-vs-modules
All rights reserved
 
  
 