By apirak on Tag:

พอดีตอนนี้ที่ทำงานกำลังพยายามเขียน Plugin ExtJS โดยพยายามให้เป็น Convention over Configuration พยายามแทรกเข้าไปกับ model เลย ตอนนี้การสร้าง table ยังใช้การประกาศตัวแปรขึ้นมาอยู่ แต่หันไปเห็น belong_to :table, has_many :tables เลยอยากสร้าง method แบบนี้ได้บ้าง

พยายามเลียนแบบ ActiveRecord::Base อยู่พักใหญ่ แต่ได้ความว่าน่าจะใช้ self เพื่อทำให้เป็น class method แต่ก็ยังไม่สำเร็จ

class Foo
@name

  def self.included(base)
    base.extend(ClassMethods)
  end

  def x
    puts @name
  end

  def self.y(name)
    @name = name
    puts "y"
  end

  def self.z
    puts "z"
  end
end

class Goo < Foo
  z
  y :goo
end

กว่าจะทำให้ Goo รู้จัก z ได้แทบแย่ แต่พอทดลอง


g = Goo.new
=> #

ตอนนี้เริ่มเอะใจว่าทำไมมันไม่ puts "z" แต่ก็ลองต่อไป


g.x
nil
=> nil

ที่นี้ชัดเลยว่ามันไม่ยักกะใส่ค่าให้ @name ตอนนี้เลยมืด ∞ ด้านเลยครับ

Apirak.com Panatkool

ps. ใน blognone ใช้ Module Geshi ได้หรือเปล่าครับ

Hiring! บริษัทที่น่าสนใจ

Carmen Software company cover
Carmen Software
Hotel Financial Solutions
Next Innovation (Thailand) Co., Ltd. company cover
Next Innovation (Thailand) Co., Ltd.
We are web design with consulting & engineering services driven the future stronger and flexibility.
KKP Dime company cover
KKP Dime
KKP Dime บริษัทในเครือเกียรตินาคินภัทร
Kiatnakin Phatra Financial Group company cover
Kiatnakin Phatra Financial Group
Financial Service
Fastwork Technologies company cover
Fastwork Technologies
Fastwork.co เว็บไซต์ที่รวบรวม ฟรีแลนซ์ มืออาชีพจากหลากหลายสายงานไว้ในที่เดียวกัน
Thoughtworks Thailand company cover
Thoughtworks Thailand
Thoughtworks เป็นบริษัทที่ปรึกษาด้านเทคโนโยลีระดับโลกที่คว้า Great Place to Work 3 ปีซ้อน
Iron Software company cover
Iron Software
Iron Software is an American company providing a suite of .NET libraries by engineer for engineers.
CLEVERSE company cover
CLEVERSE
Cleverse is a Venture Builder. Our team builds several tech companies.
Nipa Cloud company cover
Nipa Cloud
#1 OpenStack cloud provider in Thailand with our own data center and software platform.
Bangmod Enterprise company cover
Bangmod Enterprise
The leader in Cloud Server and Hosting in Thailand.
CIMB THAI Bank company cover
CIMB THAI Bank
MOVING FORWARD WITH YOU - CIMB is the leading ASEAN Bank
Bangkok Bank company cover
Bangkok Bank
Bangkok Bank is one of Southeast Asia's largest regional banks, a market leader in business banking
MuvMi (Urban Mobility Tech Co.,Ltd.) company cover
MuvMi (Urban Mobility Tech Co.,Ltd.)
Shape the future of urban mobility towards affordable, clean, and safe solutions
T.N. Digital Solution Co., Ltd. company cover
T.N. Digital Solution Co., Ltd.
TNDS has been involving in every first move of banking’s major digital transformation.
KBTG - KASIKORN Business-Technology Group company cover
KBTG - KASIKORN Business-Technology Group
KBTG - "The Technology Company for Digital Business Innovation"
Siam Commercial Bank Public Company Limited company cover
Siam Commercial Bank Public Company Limited
"Let's start a brighter career future together"
Icon Framework co.,Ltd. company cover
Icon Framework co.,Ltd.
Global Standard Platform for Real Estate แพลตฟอร์มสำหรับธุรกิจอสังหาริมทรัพย์ครบวงจร มาตรฐานระดับโลก
REFINITIV company cover
REFINITIV
The Financial and Risk business of Thomson Reuters is now Refinitiv
H LAB company cover
H LAB
Re-engineering healthcare systems through intelligent platforms and system design.
The Gang Technology Co., Ltd. company cover
The Gang Technology Co., Ltd.
We're a Digital Agency that helps our customers transform their business into digital with ease.
LTMH company cover
LTMH
LTMH มุ่งเน้นการพัฒนาผลิตภัณฑ์ที่สามารถช่วยพันธมิตรของเราให้บรรลุเป้าหมาย
Seven Peaks company cover
Seven Peaks
We Drive Digital Transformation
Wisesight (Thailand) Co., Ltd. company cover
Wisesight (Thailand) Co., Ltd.
The Best Choice For Handling Social Media · High Expertise in Social Data · Most Advanced and Secure
MOLOG Tech company cover
MOLOG Tech
We are Modern Logistic Platform, Specialize in WMS, OMS and TMS.
Data Wow Co.,Ltd company cover
Data Wow Co.,Ltd
We enable our clients to realize increased productivity by solving their most complex issues by Data
LINE Company Thailand company cover
LINE Company Thailand
LINE, the world's hottest mobile messaging platform, offers free text and voice messaging + Call
LINE MAN Wongnai company cover
LINE MAN Wongnai
Join our journey to becoming No.1 food platform in Thailand

pphetra Mon, 14/07/2008 - 12:29


def self.y(name)
@name = name
..
end

ตัวแปร @name ข้างบนมันอยู่ใน scope ของ Class Instance variable ซึ่งเป็นคนละ scope กับ @name ที่อยู่ใน method x

ใน class method เราไม่สามารถ access พวก instance variable ได้
วิธีทำที่ไกล้เคียงกับที่ apirak ยกตัวอย่างมาก็คือ ใช้ closure ช่วย

class Foo
def self.y(name)
define_method :x do
name
end
end
end

class Goo < Foo
y :goo
end

g = Goo.new
=> #Goo:0x10b11f0
g.x
:goo

ตรง define method เขียนให้สั้นลงแบบนี้ก็ได้

define_method :x, lambda {name}

sid Mon, 14/07/2008 - 16:12

แอบสงสัยอย่างหนึ่งตรง self.y ใน class Foo ที่ทำให้ class Goo สามารถสั่ง

y :goo

ถ้าจะทำแบบนี้ได้ y ต้องกำหนดเป็น class method อย่างเดียวเลยใช่มั้ยครับ พอดีลองไปไล่ดู Validation ใน ActiveRecord ก็เห็นพวกคลาสอย่าง validate_presence_of สร้างเป็น instance method หรือจริง ๆ แล้วเค้าเอาพวกนี้ไปประกอบกับคลาส Base ให้เป็น class method ทีหลังครับ

pphetra Mon, 14/07/2008 - 17:23

In reply to by sid

ลองทำโปรแกรมข้างบนให้อยู่ใน pattern เดียวกับที่ validates_presence_of ใช้ดูนะครับ

เริ่มด้วยการประกาศ module

module MyDSL
def self.included(klass)
klass.extend ClassMethods
end

module ClassMethods
def y(name)
define_method :x, lambda { name }
end
end
end

เวลาใช้

class Foo
include MyDSL
end

class Goo < Foo
y :goo
end

จริงถ้าไม่อยากเยิ่นเย้อ ก็ไป include ไว้ใน Goo ตรงๆ ก็ได้
เช่น

class Goo
include MyDSL
y :goo
end

mk Mon, 14/07/2008 - 16:46

เปิด GeSHi ให้แล้วนะครับ รายละเอียดดูใน group