ค้นหาบล็อกนี้

วันจันทร์ที่ 3 พฤษภาคม พ.ศ. 2553

Basic::Ruby (part I)

everything in Ruby is object

example 1 is an object

try 1.class // output is Fixnum

example "BENZ" is an object

try "BENZ".class

someObj.someMethod
someObj ->name of object that reference to the object
someMethod ->to message to another object


puts ->to print string on the screen
gets ->to get string in commandline

ปล. method "gets" รับ "\n"ด้วยดังนั้น เวลารับให้เราตัด "\n"ออก หากเราต้องการนำไปเปรียบเทียบ string ให้ทำดังนี้
str=gets.chomp


case number
when number value interval
-----------------------
when number value interval
-----------------------
end

assignment statement

a=b

Control flow construct

if boolean_test
-------------------------
else
------------------------
end

unless boolean_test

else

unless จะกลับกับ if ตรงที่ ถ้า boolean test ของ if เป็นจริง จะทำ statement ใน if แต่ถ้า เป็น unlessถ้า boolean test เป็นเท็จ จะทำ statement ใน unless เช่น
unless number >=5
puts "number is less than 5"
else
puts "number is greater than 5"
end
หรือ พูดอีกอย่างก็คือ ผลลัพธ์ที่นอกจาก number >=5 ให้ทำ ในstatement unless


boolean => true , false

Boolean Constructs
> ,< ,>=,<=, !=,== <=> หมา่ยถึงการเปรียบเทียบระหว่าง2value ถ้า ตัวหน้า.....ตัวหลัง (มากกว่า เป็น1 ,เท่ากันเป็น 0 ,น้อยกว่าเป็น -1)
เช่น
a=10
a<=>9 // output is 1
a<=>10 //output is 0
a<=>11 //output is -1

Boolean Operator
and ,&&
or , ||

Loop Block and iterator

while booleantest
statement
increament
end
while->ทำจนกว่าจะเป็นเท็จ


until booleantest
statement
increament
end

until->ทำจนกว่าจะเป็นจริง

for i in a .. n
statement
end

for ->to do somthing in interval value for loop


number.times {|i|
statement}

number ->
object of class fixnum
times ->
method of class but in this example doesn't have any thing to do
{ } the block that to be execute

Array in ruby
can contain difference type
เช่น
a=["benz",2,3,]
เข้าถึงโดย a[0]...a[n-1]
a.length
a.each{|var | puts var}

class and object in ruby

class
Test
def hello
puts
"Hello World"
end
end

create object
t=Test.new
Test.new ->create object that allocate memory and t points to that memory

InstanceVariable VS class Variable
InstanceVariable -> @ variable that associate with object
ClassVariable -> @@ variable that associate with class


setIvar(a)
getIvar ->
is nill if you instanciate but you don't set value for instance variable
setCvar(b)
getCvar ->
if you initialize class variable in class you can get value when you instantiate object

How to access Instance variable in ruby


attr_reader :avar
attr_reader ->
method call that expect a symbol
:avar ->
symbol ที่สอดคล้ิองกับตัวแปร instance ที่เราต้องการset ค่าของมัน

attr_accessor
:avar
you declare like this . you can access "avar "to set a value by use
instance.avar=value

attr_writer :avar
...

Flow Control

เนื่องจาก ใช้ ruby interpreter ดังนั้นเริ่มจาก ทำการ load "..." ก่อน หากเราวาง Statement ไว้ใน load ก่อน ก็จะทำตาม statement ไล่ลงมา แล้ว execute หากมีการประกาศclass ถ้าในclass นั้นมีการวาง statement ในส่วนของการประกาศ class variable ก็จะทำด้วย ส่วนใน การประกาศ method จะไม่ทำ จากนั้นก็ทำตามstepมาเรื่อยๆ

Duck Typing
เกิดจากการส่งพารามิเตอร์เข้าไปในmethodซึ่งพารามิเตอร์ที่ส่งไปดันใช้ methods ในmethodนั้น ได้ เช่นส่งString เข้าไปในmethod 2ตัวโดยใช้ operator "<<"เป็นการ append Stringใน ruby แต่หา่กพารามิเตอร์ที่ส่ง ไปนั้น เป็น array integer กับ integer ก็จะเป็นว่าเราทำการadd integer เข้าไปใน array integer ซึ่งใช้ method "<<" เหมือนกันไำด้ จึงมีคำกล่าวที่ว่า
"ถ้าเห็นนกที่เดินได้เหมือนเป็ด ว่ายน้ำได้เหมือนเป็ด
ร้องได้เหมือนเป็ด นกตัวนั้นเป็นเป็ด”
ยังไม่ค่อยเข้าใจความหมายซักเท่าไหร่วันไหนคิดได้จะมาอธิบายให้





ไม่มีความคิดเห็น:

แสดงความคิดเห็น