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/
ไม่มีความคิดเห็น:
แสดงความคิดเห็น