ruby ไม่มี primitives data type

  • 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.

ภาษา Java, C# หรือ C++ มี int, float, boolean เป็นต้น ที่เป็น primitive data type ไม่ได้เป็น Object ทำให้หลายครั้งถ้าเราต้องการ Object Integer เราต้องแปลง primitive ให้เป็น Object ซะก่อน แต่สำหรับ Ruby ขอให้ programmer มั้นใจว่าทุกอย่างในภาษานี้เป็น Object ทั้งหมด ไม่มี primitive data type ให้สับสนดังนั้นเราจึงเรียกใช้งานตัวเลขได้ตรงๆ เหมือนการเรียกใช้ Object ได้เลย

3.7.round    # Give us 4.0
3.7.truncate # Give us 3
-123.abs     # Give absolute value, 123
1.succ       # Successor, or next number, 2

เราสามารถตรวจสอบได้ว่า ตัวแปลของเราเป็น class อะไร

7.class  # Give us Fixnum
88888888888888.class # Give us Bignum
3.14159.class # Give us Pi :P Sorry. It give us Float

ทุก Object ต่างสืบทอดมาจาก class Object และได้ความสามารถพื้นฐานของ Object มาด้วย

'hello'.nil?  # false
44.to_s # Return a two-character string '44'
'hello'.to_s # Return 'hello'

Object สามารถบอกได้ว่าตัวเองเป็นค่าว่างหรือไม่ และสามารถ Override method ของแม่ได้เหมือน toString ใน java

ย้าย Codenone

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

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