Skip to content

Instantly share code, notes, and snippets.

@chrisza4
Last active March 19, 2020 11:29
Show Gist options
  • Select an option

  • Save chrisza4/a5eea43a5c05fec2a70983439af9fd03 to your computer and use it in GitHub Desktop.

Select an option

Save chrisza4/a5eea43a5c05fec2a70983439af9fd03 to your computer and use it in GitHub Desktop.
func
class OtherThing
def initialize(input)
@context = input.to_context
end
def do_stuff
run_with @context.get_those
end
end
class AnotherThing
def initialize(input)
@context = input.get_that
end
def to_context
# Code ยาวๆ ซัก 8 บรรทัด
@context.do_this + something
end
end
this_thing = ThisThing.new(request)
that_thing = ThatThing.new(this_thing)
those_things = ThoseThings.new(that_thing)
another_things = AnotherThing.new(those_things)
binding pry;
other_thing = OtherThing.new(another_things)
# เรารู้ว่า other_thing ต้องการ input.to_context.get_those แต่จะต้องไปปรับอะไรตรงไหนให้มันได้แบบที่ต้องการกันนะ หายากจัง..
this_thing = createThisThing(request)
that_thing = createThatThing(this_thing)
those_things = createThoseThing(that_thing)
another_things = createAnotherThing(those_things)
console.log(another_things) // ตรงนี้ได้ JSON เช่น {"my_id": 111, "your_id": 222 } แล้วเราสามารถแก้ได้เลย
other_thing = createOtherThing(another_thing)
// ใน console เล่นได้เต็มที่
> createOtherThing({"my_id": 111, "your_id": 222 })
> createOtherThing({"my_id": 111, "your_id": 333 })
> createOtherThing({"my_id": 111, "your_id": 444 })
// จริงๆ ผมแค่ชอบที่บรรทัดที่ 6 มันเป็น Raw Data ซึ่งจริงๆ เข้าใจว่าทุกภาษาที่เป็น Functional programming จะได้มาอยู่แล้ว
// ต้องบอกว่าเวลาเขียนแบบให้ Subsystem คุยกันผ่าน Message ที่เป็นข้อมูลเพียวๆ แบบไม่ได้แอบมี Method แถมมาด้วย จะได้ข้อดีแบบนี้
// ซึ่งถ้าทำตาม Original OOP ของ Alan Kay ก็จะคุณสมบัติแบบนี้มา เช่นกัน ที่สามารถ Log message แก้นิดหน่อย แล้ว Simulate สถานการณ์ต่างๆ ได้
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment