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

วันอังคารที่ 4 พฤษภาคม พ.ศ. 2553

Basic::Ruby (part II)

Basic Ruby (partII)

constant variable &scope

scope ->where variable has a value (::)
constant -> delare with a capital letter
initialize -> to do when instantiate happen

Expression
multiple expression in one line
a=1; b=2; c=3

command expansion
`date`
`ls`

Assignment

a=11;b=12;;c='e'
puts "a=#{a} b=#{b} c=#{c}" //in double quote แตุ่ถ้าเป็น single quote จะใช้ไม่ไ้ด้
output is a=11 b=12 c=e
parallel -> a=b=c
concatString +
puts 'a'+'b'
print ->print a line and no carriage return
puts ->prints out the carriage return


ri searchcontent ->ดูคำอธิบายการใช้ library







Expression Loop
while ...
end

9.times do |var| //mean start form 0 to 8
print "#{var}" // 9 is number of times
end

puts =>is the same output as Enter

1.upto(9) do |var| // 1 to 9
print "#{var}"
end

1.step(9,3) do |var| //start at 1 to 9 ,step is 3
print "#{var}"
end

Module ->a wayt to encapsulate a bunch of different code ซึ่งเป็น concept
ของ namespace
เนื่องจาก เรามี การประกาศmethod ไว้มากมายอาจซ้ำกันได้ในหลายๆไฟล์
ถ้าเรา ประกาส keyword module ไว้ก็จะทำให้ทราบว่า method นั้น เป็นของ Moduleไหน

module testA
def A.print_name
puts "hello"
end
end
module testB
def B.print_name
puts "hi"
end
end
การอ้างอิง TestA.print_name
TestB.print_name
Mix -ins with module
ก็คล้ายๆกับการประกาศและสร้างnamespace ให้กับ class ซึ่งก่อนอื่นเราจะสร้าง module
ของ method ที่เราต้องการจากนั้น นำไปใช้กับ class เพื่อ

Block

เป็น a bit of code ที่สามารถส่งไปให้กับ iterator เพื่อทำการ executeได้ เช่น
def time
yield
yield
end

time {puts"Hello"}

เมื่อมีการเรียกใช้ method time เมื่อ เจอ yield จะกลับไปexecute ที่ block {puts......} จากนั้นจะกลับมาทำต่อใน time ใหม่ ไปเรื่อยๆจนถึง end โดยเราสามารถส่งพารามิเตอร์ไปให้block ได้
เช่น

def time
yield 1
yield 2
end
time {|x| puts "hello #{x}"} หรือ time {|x| puts x}
หรือ สามารถใช้ อีก รูปแบบหนึ่งก็ได้
time do |x|
puts x
end

Basic Exception
def
func
begin
recue
ZeroDivisionError
.......................
end
end

Basic String::
str="It's a string " is the same as %q/This is a string/ can use #{a} replaced by variable
str='It's a string' is the same as %Q/ This is a string/ use#{a} is string
str.capitalize => the first one is a capital letter.



How to use string in long sentence
<<
END_OF_STROING
"---------------------------------"
END_OF_STROING

str =" hello "
str.downcase
str.upcase
//replace in new string
str.lstrip =>"hello "
str.rstrip => " hello"
str.strip =>"hello"
//use ! to replace in old string
example = > str.strip!

str.length
str.insert(5," world") // "hello world"

Manipulate in basic string
str="It is a book"
str.split
str.split(delimeter)
delimeter => ตัวคั่น โดยการ split จะทำการแ่บ่งตามคั่น
str.split(',',4 ) =>ทำการsplit strimg ออกตามdelimiter (',') ซึ่งแบ่งออกเป็น4 ส่วน
str.split(/ /) แบ่งตามจำนวน space bar
...เยอะมาก
ออกมาเป็นarray

str.slice(i..n)
str.index('h')
str.replace "word" //replace whole str and replace in the old string
str.gsub(pattern,replacement)
str.sub(pattern,replacement)
str.include? "string" => boolean
str.to_s
str.to_i
...





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

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