Math && RUBY
นำหน้าด้วย
0b ->for binary number
such as 0b101
0-> for octal
0x ->for hex
** for power
^xor
flt.round => integer
x.floor
x.ceil
สามารถprintf("%6.2f",x) ทำได้เหมือนใน C เลย
x1.divmod(x2) //result =[x1/x2,x1%x2]
q1,q2=x1.divmod(x2)
i1=3
i1[2]=0 i1[1]=1 i1[0]=1
BigDecimal
provides similar support for very large or very accurate floating point numbers.
เนื่องจากการเก็บ ค่า ตัวเลขจะเก็บในรูปของ string ที่เป็นเลข ASCII
แล้วทำการ converse เป็นตัวเลขเมื่อใช้มันอีกที เช่น มีเลข 8 ถ้าเป็นแบบ float ธรรมดา
โดยไม่ใช้ bigDecimal จะแปลงเป็นbinary =>1000 แต่หากเป็น bigDecimal
จะเก็บเป็นASCII ของ 1 0 0 0 ซึ่งอาจทำให้่เสีย performance แ่ต่มัน accuracy กว่ากันเยอะ
require 'bigdecimal'
BigDecimal.new("1.2")
require 'bigdecimal/math'
include BigMath
BigDecimal::PI(number หลัง ทศนิยม)
BigDecimal.sin(BigDecimal.PI(radian), ...)
BigDecimal.sin(BigDecimal.new('1'), ...)
ปกติ
Math::PI =>
Math.sin(radian)
Math.sin(Math::PI)
...
8.to_s(2) => "1000"
8.to_s(8) =>"10"
...
require'mathn'
8.gcd2(120) //
36.lcm(120) //
-----------------------------------
class Prime
Prime.new
Prime.next
rand ->pseudo random =>using statistic sampling random because the natur e source for random is the decay but electron can not predict so you use a large sampling size to random
n=rand //random in range 0.0 -1.0
n=rand(10)
SYMBOL in RUBY
=>class Symbol
เราต้องมองก่ิอนว่าค่า 1 , 2 , 3 นั้นมีค่าเดียวใน ระบบ
เช่นเมื่อลอง พิมพ์ 1.object_id => สมมติว่า ได้ 3
2.object_id => สมมติว่า ได้ 5
3.object_id => สมมติ ว่าำได้ 7
ถ้าเราบอกว่า
m=1 //เมื่อ m คือ variable นึกเสียว่า มี setของ ตัวเลขแล้วตัวแปร
m ชี้เลข 1 อยู่
ซึ่งถ้าลอง m.object_id // ได้ 3 จะเห็นว่า m มันชี้ค่า 1 (เนื่องจากมีการอ้างอิง object _id เดียวกันกับ1 ซึ่งเป็นตัวระบุความแตกต่างของสิ่งที่เราประกาศ)
m=2
m.object_id//ได้ 5 เปลี่ยนไปชี้ที่ ค่า 2 (เนื่องจากมีการอ้างอิง object _id เดียวกันกับ2 )
จากส่วนนี้ทำให้เห็นว่า ตัวแปร(variable) สามารถชื้ำไปได้หลายค่า ซึ่งเราสามารถ assign ค่าได้
n=2
n.object_id //ก็ยังได้ 5 เปลี่ยนไปชี้ที่ ค่า 2(มี object_id =5)
m==n //ได้ true รู้ได้อย่างไรว่า เท่ากัน ก็อ้างอิง object_id เดียวกัน
จากส่วนนี้ จะได้ว่า ตัวเลข 2 นี้จะมีค่าเดียวโดยที่ถ้าเราประกาศตัวแปรอะำไรก็ตาม
แล้วassignค่าเท่ากับ 2 มันก็จะชี้มาที่ตัวเลข 2 ตัวเดียวกัน
ดังนั้น symbol ที่ต้องการจะพูดถึงนี้ จึงเป็นเหมือน กับ Literal ทีี่่่่มีได้ค่าเดียว และแน่นอนเราไม่สามารถ assignค่าได้ โดยใน ruby เราจะประกาศดังนี้
:symbol //ก็คือค่า :symbol เหมือนกับเราประกาศ
3 //ผลลัพธ์ ก็คือ ค่า 3
ลองพิสูจน์ดูว่า m=:test
n=:test
m.object_id == n.object_id หรือเปล่า
วิธีดู object _id ของทั้งใน symbol table
Symbol.all_symbols
Range in Ruby
=>class Range
r= 4..8 //shold assign range in variable
r.to_a // [4 ,5,6,7,8]
r.each{|x| print x} //45678
a='4'..'8'
a.to_a // ["4","5","6","7"]
a.each{|x| print x} //45678
f=1.0..2.0
Float Array is not support methods each
f.each{|x| puts x}//error ,it is a infinity loop
t=4...8 //4567
t.include?(8) // no
Getting the Time & Date
=>class Time
Time.mktime(year,month,day)
s=Time.now
puts s.strftime("%A") //print full day
Doing math with Time and Date
now =Time.now
hbd=Time.mktime(1989,02,11) //my birth day
เนื่องจาก class Time มี method +-... เราสามารถใช้ในการ ความแตกต่้างของเวลา
ในหน่วยวินาทีได้ึคือ
diff=now-hbd // return Float that refer to seconds of difference time
diff+=1 // increase time in a second
Formating Date and Time
now=Time.now
now.strftime(string ) // string="your month:: %m" same as c-function
Array/Hashes
Array is holding a data in Ruby you can store a difference data
arr=[1,"hello",102.21]
arr จะเป็นตัวชี้ไปที่ memoryเริ่มต้นในการจองตัวแปรแบบ array
declaration
x=[]
x<<1> x=Array.new //[]
x=Array.new(3) // [nil],[nil],[nil]
x=Array.new(3,"hello") //["hello","hello","hello"]
x.capitalize! //["hello","hello","hello"] change All
x=Array.new(5){"hello"} //use block if you know you willm mo
x[0].capitalize! //change x[0]
x=[1,"hello","123.45"]
access array
a[0]
a[1]
...
a[n]
methods
x.first
x.last
x.length
x.size
Sorting Array
in the same type data
in difference data
x=['1',5,'3',"2",4]
x.sort {|x,y| x.to_s<=>y.to_s }
Searching Array
find is Synomym of detect =>หาเจอตัวแรกเอาเลย
find_all is Synonym of select =>เอาหมดทุกตัวที่ตรงตามเงื่อนไข
grep(reg_expression) เช่น
reg_expression จับหลักจากการสังเกตได้ว่า
str=[]
str.methods.grep(//) => select all
str.methods.grep(/\bm/) => ขึ้นต้นด้วยตัวm
str.methods.grep(/p\b/) => ลงท้ายด้วย ตัว p
str.methods.grep(/test/) =>["test_1","test_2"]
str.methods.grep(/test/){|v| IO.const_get(v)}
str.min
str.max
num=Array.new
num.each{|x| p x} //iterating Trough an Array
num.each do |x|
if a==2
p "hello"
end
end
you can use
num.reverse_each{|v| p v}
array is a special type of Hash but Hash is not ordered หมายความว่า
เราไม่ทราบลำดับในการ get value เนื่องจากค่า keyไม่จำเป็นต้องเรียงลำดับ
Howto use key to get value
color=Hash.[](:white =>"#FFFFFF", :black=>"#000000")
color[:white] // "#FFFFFF"
color =Hash.[]("white =>"#FFFFFF", "black" =>"#000000")
color["black"] //"#000000"
color=Hash.[](0=>"#FFFFFF",1=>"#0000000")
OR
color=Hash.[](:white,"#FFFFFF", :black,"#000000")
color=Hash.new
color=Hash,new(10)
color.[]=(key,value)
color.delete(key) //delete key and value in Hash
color.each{|key,value| puts "#{key} : #{value}"}
color.each_key{|key| puts key}
color.each_value{|value| puts value}
Sorting in Hash
เมื่อเราทำการ sort Hash แล้ว จะได้ออกมาเป็น Array ของ2ค่าคือ key and value
เนื่องจากในการsort key หาก เราประกาศHash ที่มี Key เป็น symbol เราต้องแปลงมัน
เป็น string ก่อนแล้วจึงจะเปรียบเทียบ Key โดยใช้ block แล้วใช้ operator "<=>"ในการเปรียบเทียบ
ระหว่าง ตัวแรก กับตัวถัดไปซึ่งรับมาในรูปของ iterator variableเช่น
color=Hash.[](:red =>"#FF0000",:green=>"00FF00")
color.sort{|a,b| a<=>b} //a is key , b is next key
ถ้าหากkey เป็น string หรือ ที่ไ่ม่ใช่symbol ก็ sort เลยได้
new has=x.merge(color)
y=[1,2,3,4,5]
y_hash=Hash[*y]
Set is a semilar to an array but It's unordered
require 'set'
a=Set[1,2,3]
b=Set[3,4,5]
b.intersection a
b&a
b-a
a-b
********************
a.union b
a| b
a+b
*********************
a.subset? b
a.delete(element)
y=[1,2,3,4,5]
new_set=Set[y]
Stack and Queue
จะใช้ array ในการ implement แต่มีmethods ให้ใช้คือ
FOR STACK
x.push(1,2,3)
x<<4
Accessing Other Program on the system
system("nodepad.exe")
system("cmd /c dir")
Commandline Arguments
-------------------------------------
->use GetoptLong class
require 'getoptlong'
-parser=GettoptLong.new
parser.set_options(["-h","--help,GetoptLong::NO_ARGUMENT"]
,["-t",'--test',GetoptLong:: REQUIRED_ARGUMENT])
opt,arg=parser.get
puts arg
arg.to_i.times{|x| puts"Hello"}
ถ้ารับหลาย argument แนะนำ
n=ARGV.size
ARGV.each do|arg|
arg.to_i.time{|arg| puts arg}
end
Environment Variable for window
-> ENV (is a hash that content about environment Variable)
Network client
require 'net/http'
content =Net::HTTP.new("http://www.google.co.th",80)
respond,data=content.get("/index.html",nil)
puts data //so data is a string
you can copy content in the web page in the file
File.open("test.html","w") do |file|
file.puts data
end
require 'resolv'
Resolv::DNS.each_address("www.google.co.th"){|x| puts x} //so you get a DNS server
reference resource ::http://www.ruby-doc.org/stdlib/
ค้นหาบล็อกนี้
วันศุกร์ที่ 7 พฤษภาคม พ.ศ. 2553
วันพุธที่ 5 พฤษภาคม พ.ศ. 2553
Basic::Ruby (part III)
FILE IN RUBY
use file open
-> hava a file class
filename= "test9.cpp"
File.open(filename) do |files|
while line= files.gets
puts line
end
end
files.gets => จะ return แต่ละบรรทัด มาให้
File.writable? filename
File.readable? filename
File.executable? filename
File.exist? filename
do not close file any where
def test2
yield 1
yield 2
end
test2 {|x| if x>0 then puts x else puts "error" end }
หรือ
test2 {|x| if x>0 ; puts x else puts "error" end }
test2 {|x| if x>0 : puts x else puts "error" end }
...
Easy way to read file
filename ="helloworld"
IO.foreach(filename){|line|puts line}
str=IO.read(filename) //read file into the string
puts str
arr=IO.readlines(filename) // read line of file into the Array
How to write a file
File.open(" ......... ","w") do |filecontent|
filecontent.puts "hello world"
end
Binary file vs Text file
Text File โดยทั่ว ๆ ไปโปรแกรมจะต้องมองเรื่องของการ encode/decode ไฟล์ในการ อ่านและเขียน การ encode/decode นี้ก็เช่นวิธีการเก็บไฟล์เป็น ภาษาไทย ภาษาจีน หรือ ภาษาอังกฤษ นั่นเอง ซึ่งแต่ละภาษาจะมีวิธีการเก็บที่แตกต่างกันเช่น ASCII, ISO, Unicode, JIF และอื่น ๆ
Binary File โดยทั่ว ๆ ไปจะหมายถึงการที่โปรแกรมมองเรื่องของการอ่านและเขียนไฟล์ในรูปแบบของ byte-to-byte หรือก็คืออ่านเป็น byte และเขียนเป็น byte โดยไม่สนใจการ encode/decode ต่าง ๆ เลยครับ
ruby ให้เราอ่าน textfile และแก้ไข text file ได้ ส่วน binary file สามารถอ่านได้แต่ จะต้องหาlibrary มาลงจึงจะสามารถ จัดการ กับ binary file ได้
Work with Directory
//use Dir class
Dir.mkdir(path) =>create directory in that path
Dir.pwd
Dir.entries(Dir.pwd)
Dir.entries('.')
FileUtils
-------------------------
require 'fileutils' //you can use file utility to use a Unix - Dos command
such as=> Fileutils.pwd()
Read and Write file(.csv) -> format for holding data
require 'csv'
CSV.open(filename,'w') do|data|
data <<['...','.....','......']
end
CSV.open(filename,'r') do |data|
p data
end
p is a shotcut of "puts"
Regular Expression is a string to match a particular thing that
you're searching for
such as str.gsub(/[a-z]/,"*") output=>
use file open
-> hava a file class
filename= "test9.cpp"
File.open(filename) do |files|
while line= files.gets
puts line
end
end
files.gets => จะ return แต่ละบรรทัด มาให้
File.writable? filename
File.readable? filename
File.executable? filename
File.exist? filename
do not close file any where
def test2
yield 1
yield 2
end
test2 {|x| if x>0 then puts x else puts "error" end }
หรือ
test2 {|x| if x>0 ; puts x else puts "error" end }
test2 {|x| if x>0 : puts x else puts "error" end }
...
Easy way to read file
filename ="helloworld"
IO.foreach(filename){|line|puts line}
str=IO.read(filename) //read file into the string
puts str
arr=IO.readlines(filename) // read line of file into the Array
How to write a file
File.open(" ......... ","w") do |filecontent|
filecontent.puts "hello world"
end
Binary file vs Text file
Text File โดยทั่ว ๆ ไปโปรแกรมจะต้องมองเรื่องของการ encode/decode ไฟล์ในการ อ่านและเขียน การ encode/decode นี้ก็เช่นวิธีการเก็บไฟล์เป็น ภาษาไทย ภาษาจีน หรือ ภาษาอังกฤษ นั่นเอง ซึ่งแต่ละภาษาจะมีวิธีการเก็บที่แตกต่างกันเช่น ASCII, ISO, Unicode, JIF และอื่น ๆ
Binary File โดยทั่ว ๆ ไปจะหมายถึงการที่โปรแกรมมองเรื่องของการอ่านและเขียนไฟล์ในรูปแบบของ byte-to-byte หรือก็คืออ่านเป็น byte และเขียนเป็น byte โดยไม่สนใจการ encode/decode ต่าง ๆ เลยครับ
ruby ให้เราอ่าน textfile และแก้ไข text file ได้ ส่วน binary file สามารถอ่านได้แต่ จะต้องหาlibrary มาลงจึงจะสามารถ จัดการ กับ binary file ได้
Work with Directory
//use Dir class
Dir.mkdir(path) =>create directory in that path
Dir.pwd
Dir.entries(Dir.pwd)
Dir.entries('.')
FileUtils
-------------------------
require 'fileutils' //you can use file utility to use a Unix - Dos command
such as=> Fileutils.pwd()
Read and Write file(.csv) -> format for holding data
require 'csv'
CSV.open(filename,'w') do|data|
data <<['...','.....','......']
end
CSV.open(filename,'r') do |data|
p data
end
p is a shotcut of "puts"
Regular Expression is a string to match a particular thing that
you're searching for
such as str.gsub(/[a-z]/,"*") output=>
วันอังคารที่ 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
...
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
<<
"---------------------------------"
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
...
วันจันทร์ที่ 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 "<<" เหมือนกันไำด้ จึงมีคำกล่าวที่ว่า "ถ้าเห็นนกที่เดินได้เหมือนเป็ด ว่ายน้ำได้เหมือนเป็ด
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 "<<" เหมือนกันไำด้ จึงมีคำกล่าวที่ว่า "ถ้าเห็นนกที่เดินได้เหมือนเป็ด ว่ายน้ำได้เหมือนเป็ด
ร้องได้เหมือนเป็ด นกตัวนั้นเป็นเป็ด”
ยังไม่ค่อยเข้าใจความหมายซักเท่าไหร่วันไหนคิดได้จะมาอธิบายให้interpreter vs compiler
intrepreted program -> เป็น โปรแกรมที่สามารถ execute ได้ line by line (หมายถึง one statement )
โดยจะถูกแปลงเป็นmachine code และ execute มันทันที ซึ่งเราสามารถเปลี่ยนมันได้ง่ายในขณะที่โปรแกรมกำลัง run อยู่ แต่จะมีข้อเสียคือ เสียเวลาในการโปรเซส เนื่องจากต้องมาคอยแปลงเป็น Matchine code ในแต่ละทุกๆบรรทัดแล้วทำงานทันที แทนที่จะอ่านมาทีเดียวแล้วค่อยจัดการทั้งโปรแกรม
compiled program -> จะเป็นโปรแกรมที่้compiler จะconvert มันเป็น machine code เพียง stepเดียว ซึ่งสุดท้ายแล้ว จะได้ obj code ซึ่งประกอบด้วย binary (0,1)ซึ่ง จะบอก microprocessor ว่าคำสั่งนี้หมายถึงอะำไร และมีการลิงค์ส่วนต่างเช่น วิธีในการแสดงผลทางหน้าจอ ว่าจะมีการเรียกprint อย่างไร
virtual machine ->เมื่อเราเขียนโปรแกรมเช่นjava ซึ่งเราเขียนแล้ว save เป็น.java จากนั้นเราใช้java compiler ทำงาน จะได้ java bytecode ซึ่งเป็น intermediate form ซึ่งจะสามารถรันได้ ใน interpreterที่เฉพาะ แล้วจึงค่อยรัน byte codeใน java interpreter ซึ่งจะมีการ optimize code ให้สามารถรันได้ใน ทุกๆmachine เนื่องจาก interpreterนั้นขึ้นกับ machine นั้น
โดยจะถูกแปลงเป็นmachine code และ execute มันทันที ซึ่งเราสามารถเปลี่ยนมันได้ง่ายในขณะที่โปรแกรมกำลัง run อยู่ แต่จะมีข้อเสียคือ เสียเวลาในการโปรเซส เนื่องจากต้องมาคอยแปลงเป็น Matchine code ในแต่ละทุกๆบรรทัดแล้วทำงานทันที แทนที่จะอ่านมาทีเดียวแล้วค่อยจัดการทั้งโปรแกรม
compiled program -> จะเป็นโปรแกรมที่้compiler จะconvert มันเป็น machine code เพียง stepเดียว ซึ่งสุดท้ายแล้ว จะได้ obj code ซึ่งประกอบด้วย binary (0,1)ซึ่ง จะบอก microprocessor ว่าคำสั่งนี้หมายถึงอะำไร และมีการลิงค์ส่วนต่างเช่น วิธีในการแสดงผลทางหน้าจอ ว่าจะมีการเรียกprint อย่างไร
virtual machine ->เมื่อเราเขียนโปรแกรมเช่นjava ซึ่งเราเขียนแล้ว save เป็น.java จากนั้นเราใช้java compiler ทำงาน จะได้ java bytecode ซึ่งเป็น intermediate form ซึ่งจะสามารถรันได้ ใน interpreterที่เฉพาะ แล้วจึงค่อยรัน byte codeใน java interpreter ซึ่งจะมีการ optimize code ให้สามารถรันได้ใน ทุกๆmachine เนื่องจาก interpreterนั้นขึ้นกับ machine นั้น
MVC in Ruby On Rails
Ruby on rails เป็น framework ที่ใช้สำหรับสร้า่ง web application ซึ่งมีโครงสร้างแบบ MVCมาจาก-Model -> โปรแกรมที่ทำหน้าที่คำนวณงานของ เว็บ app
-View -> โปรแกรมที่ทำหน้าที่ติดต่อสื่อสารกับผู้ใช้
-Controller ->โปรแกรมที่ควบคุมการทำงานระหว่าง model กับ view
โดยขั้นตอนการทำงานมีดังนี้คือ
เริ่มจาก client ส่ง request ไปที่ web app โดยส่งต่อมาที่ controller ก่อนหน้านั้นจะต้องถูก ActionPack(ActionView->module สร้า่ง view + ActionController->module สร้างcontroller)ตีความของ request path ที่ user ส่งเข้ามาแล้วทำการโยนไปให้ action ในcontroller ซึ่งส่วนใหญ่แล้ว action ใน controller จะเรียก ไปยัง Model ซึ่งมีไว้สำหรับการคำนวณหรือติดต่อไปยัง database query ข้อมูลออกมา เมื่อโมเดลทำเสร็จจะส่้งผลกลับมาที่controller แล้วทำการ render view template(เป็นรูปแบบของViewซึ่งเป็น code ruby ที่จะใช้สำหรับสร้าง view (html page)เพื่อส่งกลับมาให้user)
วันอาทิตย์ที่ 2 พฤษภาคม พ.ศ. 2553
animation maya: แบบที่1
วันนี้ไปดู iron man มา effect เจ๋งโคตร แต่ตอนจบ ตัวโกงตายง่ายไปหน่อย โคตรเซงอุตส่าห์ลุ้นแทบตาย นึกว่าจะเก่งกว่าเยอะ เกี่ยวไรด้วยเนี่ย เกี่ยวสิวันนี้มี การทำanimation มาฝาก นั่น งงม=งง+งม ตั้งนาน สืบเนื่องมาจาก การสร้างโมเดลขึ้นมาแล้ว ใส่ boneก็แล้ว
ถึงเวลาที่จะทำให้ โมเดลของเราเคลื่อนไหวแล้ว รอช้าอยู่ไยเริ่มกันเลย
1.มาที่keyframe ที่ 1 แล้วทำการเคลื่อนที่โมเดล ณ จุดๆหนึ่ง แล้วจัดท่าของโมเดล ในท่าหนึ่ง เมื่อได้ที่แล้วกด ปุ่ม s เพื่อ ยืนยันว่า ณ frameที่1 เราให้มันเป็นแบบนี้
2.มาที่ key frame ที่ 3 แล้วทำการเคลื่อนที่โมเดล ณ จุดอีกจุดหนึ่ง แล้วจัดท่าโมเดลอีกแบบหนึ่งดังรูป แล้วกด ปุ่ม s เพื่อยืนยัน
ดังรูป
ปล.สิ่งที่สำคัญที่สุดคือการ save เนื่องจาก เราสร้าง bone ขึ้นมา ซึ่งเป็น boneนั้นมันมีหลายส่วนนั่นหมายความว่า หาเรา คลิ๊กเลือก bone ส่วนหัว แล้วทำการ save คือการบันทึกการเปลี่ยนแปลงเฉพาะ bone ส่วนนั้น ส่วน bone ส่้วนอื่นก็ต้องsave เหมือนกัน แต่สุดท้ายแล้วเมื่อมีการเล่น animationทุกอย่างก็จะเคลื่อนที่ตามเวลาที่กำหนด
ถึงเวลาที่จะทำให้ โมเดลของเราเคลื่อนไหวแล้ว รอช้าอยู่ไยเริ่มกันเลย
1.มาที่keyframe ที่ 1 แล้วทำการเคลื่อนที่โมเดล ณ จุดๆหนึ่ง แล้วจัดท่าของโมเดล ในท่าหนึ่ง เมื่อได้ที่แล้วกด ปุ่ม s เพื่อ ยืนยันว่า ณ frameที่1 เราให้มันเป็นแบบนี้
2.มาที่ key frame ที่ 3 แล้วทำการเคลื่อนที่โมเดล ณ จุดอีกจุดหนึ่ง แล้วจัดท่าโมเดลอีกแบบหนึ่งดังรูป แล้วกด ปุ่ม s เพื่อยืนยัน
ดังรูป
ปล.สิ่งที่สำคัญที่สุดคือการ save เนื่องจาก เราสร้าง bone ขึ้นมา ซึ่งเป็น boneนั้นมันมีหลายส่วนนั่นหมายความว่า หาเรา คลิ๊กเลือก bone ส่วนหัว แล้วทำการ save คือการบันทึกการเปลี่ยนแปลงเฉพาะ bone ส่วนนั้น ส่วน bone ส่้วนอื่นก็ต้องsave เหมือนกัน แต่สุดท้ายแล้วเมื่อมีการเล่น animationทุกอย่างก็จะเคลื่อนที่ตามเวลาที่กำหนด
สมัครสมาชิก:
บทความ (Atom)