Django VS Ruby on Rails

  • warning: realpath() [function.realpath]: SAFE MODE Restriction in effect. The script whose uid is 1005 is not allowed to access /tmp owned by uid 0 in /var/www/sites/sugree/codenone.com/subdomains/www/html/includes/file.inc on line 190.
  • warning: realpath() [function.realpath]: SAFE MODE Restriction in effect. The script whose uid is 1005 is not allowed to access /tmp owned by uid 0 in /var/www/sites/sugree/codenone.com/subdomains/www/html/includes/file.inc on line 190.

เอาตรงๆ เลยนะครับ Django , Ruby on Rails อันไหนเจ๋งกว่า ???

sugree's picture

ตอบตรงๆ Ruby on Rails เจ๋งกว่าในทุกด้าน ข้อเสียอย่างเดียวของ Ruby on Rails ก็คือมันเป็น Ruby

Ruby ดีทุกอย่าง เสียอย่างเดียว มันดีเกินไป ผมชื่นชม Ruby นะ มันดีจริงๆ แต่ทำใจเขียนไม่ได้ซักครั้ง

roofimon's picture

เห็นด้วยกับ sugree ขอใส่ของตัวเองเล็กน้อย
เท่าที่เล่นมา RoR > Turbo Gears > Django ครับ
Django อันนี้เป็น build in framework มีครบวิธีเขียนเข้าใจง่ายมากหมัดเด็ดคือ Admin Page แต่เวลาติดตั้งปวดหัวเล็กน้อย + เอกสารอาจอ่านยากนิดๆห้ามข้ามสำคัญทุกบรรทัด
Turbo Gear ง่ายเลย ง่ายมากๆมีหนังสือด้วย อันนี้มาแนว integrated framework เอาของคนอื่นมาผสมโรง ทำให้ได้ความหวือหวา เข้าใกล้ RoR

RoR สุดยอดชนะทุกประตู
---------------------------------แต่
def self.#{id.id2name}(*args, &block)
@__#{id.to_i}__
end
--------------------------------- ผมจอดตั้งแ่บรรทัดแรก

เดี๋ยวแฟน(ที่ไม่ชอบ) perl จะตกใจ code ของ roof
code ข้างบน ไม่ได้ใช้ special syntax แบบ perl เลยนะ
ตรง @__#{id.to_i}__ , จะใช้ชื่อที่อ่านง่ายๆก็ได้ เช่น @internal_#{id_to_i}

roofimon's picture

อุ่ยขอบคุณครับพี่

sugree's picture

ขอดูแบบสวยๆ หน่อยครับ สารภาพตามตรง ผมไม่เข้าใจซักนิด

ตรงที่ roof เขายกมา จริงๆมันเป็น String template น่ะครับ
ลองดูตัวอย่าง code นี้

class Pok
  def initialize
    @a = 1
    @b = 2
  end
 
  def self.make_dynamic(id)
   class_eval <<-EOF
 def get_#{id}
   @#{id}
 end
   EOF
  end
end
 
 
class X < Pok
  make_dynamic(:a)
  make_dynamic(:b)
end
 
 
x = X.new
puts x.get_a  # => 1
puts x.get_b  # => 2

Note: code ของ roof ยากกว่าตัวอย่างที่ผมยกมา
ตรง scope ของมันเป็นระดับ class (class method + instance variable ของ class )

roofimon's picture

อันนี้ต้นฉบับครับเอามาจาก http://www.ruby-doc.org/docs/ProgrammingRuby/ ขออภัยที่ไม่ได้บอกที่มาครับทุกท่าน
ผมผิดเอง ก่อนหน้านี้จะมีลำดับการเขียนมาเป็นระยะว่าเขียนมาได้กี่แบบครับ อันนี้เป็น code ของ Tadayoshi Funaba ครับ

def ExampleDate.once(*ids)
  for id in ids
    module_eval <<-"end_eval"
      alias_method :__#{id.to_i}__, #{id.inspect}
      def #{id.id2name}(*args, &block)
        def self.#{id.id2name}(*args, &block)
          @__#{id.to_i}__
        end
        @__#{id.to_i}__ = __#{id.to_i}__(*args, &block)
      end
    end_eval
  end
end

อภัยมา ณ ที่นี้ ที่บอกตกม้าตายคือผมความรู้ไม่ถึงครับเป็นข้อผิดพลาดส่วนตัว

sugree's picture

แบบนี้ถือว่าซับซ้อนหรือยังครับ สารภาพอีกรอบ เห็นแล้วรู้สึกเหมือนตอนแอลกอฮอล์ฉีดไปตามเส้นเลือด def ใน def ใน eval

ซับซ้อนครับ เห็นแล้วปวดหัว

ไปแกะมาแล้วครับ
ลองอ่าน comment ดูว่ามันทำอะไรบ้าง

class ExampleDate
  def hi
    puts 'orginal method was call'
    'hello'
  end
end
 
# รับ parameter เป็นชื่อ method ที่จะทำ Memoize 
# Memoize เคยมีพูดกันในโจทย์สามเหลี่ยม pascal http://www.codenone.com/node/155)
def ExampleDate.once(*ids)
  for id in ids
    # evaluate string ข้างล่างนี้เพื่อเพิ่ม method ลงไปใน class
    module_eval <<-"end_eval"
      # step1 สร้าง method alias ชื่อ __xxxx__ ที่ชี้ไปที่ method ตัวดั้งเดิม
      alias_method :__#{id.to_i}__, #{id.inspect}
 
      # step 2 สร้าง method ทับลงไปบน method เดิม เพื่อเปลี่ยนพฤติกรรม
      def #{id.id2name}(*args, &block)
 
        # step 3, method ตัวที่พึ่งสร้างใน step ที่ 2 ก็จะถูกทับใหม่ ด้วยตัวนี้แทน
        def self.#{id.id2name}(*args, &block)
          puts 'method from step 3 was called'
          # return ค่าที่ momoize ไว้
          @__#{id.to_i}__
        end
 
        puts 'method from step2 was called'
 
        # เก็บค่าที่ได้จากการ call ไปยัง method ตัวดั้งเดิม ไว้ใน instance variable
        @__#{id.to_i}__ = __#{id.to_i}__(*args, &block)
      end
    end_eval
  end
end

เวลา run ก็จะเป็นแบบนี้

irb(main):056:0> ExampleDate.once(:hi)
[:hi]
irb(mai):057:0> x = ExampleDate.new
#<ExampleDate:0xb7ccd438>
irb(main):058:0> x.hi
method from step2 was called
orginal method was call
"hello"
irb(main):059:0> x.hi
method from step 3 was called
"hello"

เคยสอบตกเพราะเจอ Perl

ความหลังฝังใจ พอบอกว่า Ruby มัน derive มาจาก Perl ผมก็เลิกอ่าน

ถ้าเอาลิงมาล้านตัว มานั่งคอมพิวเตอร์ตัวละเครื่อง จะมีลิง หนึ่งตัว ที่ "อ่าน" code Ruby รู้เรื่อง แต่ลิงทุกตัว "อ่าน" Code java ได้หมด อันนี้เป็นปรัชญา แซวกันเล่นๆ ขำๆครับ

แบบนั้นลิงทุกตัวอาจจะ "เขียน" Code Python ได้ นะครับ :-) (ขำๆ เช่นกัน)

roofimon's picture

วลีลิง นี้คุ้นๆนะ

ย้าย Codenone

ประกาศย้าย Codenone ไปใช้ Forum ของ Blognone แทนครับ ตามไปตั้งกระทู้ต่อได้ที่ Codenone Forum (รายละเอียดอ่านจากกระทู้ ย้าย Codenone ไปรวมกับ Blognone)

กระทู้เก่าๆ จะย้ายตามไปในภายหลัง ตอนนี้ปิดการโพสต์กระทู้ไว้ เหลือไว้เฉพาะอ้างอิงเท่านั้น