顯示具有 ruby 標籤的文章。 顯示所有文章
顯示具有 ruby 標籤的文章。 顯示所有文章

2008年10月21日 星期二

ruby-debugger

http://bashdb.sourceforge.net/ruby-debug.html#SEC_Top
http://thedaneshproject.com/posts/how-to-reload-your-bashrc-file/

We could create a file called .rdebugrc, and put these lines in
set autoeval             <= 不用再打 p 印 value
set autolist <= 自動列出所在行數
set autoreload <= 可以隨時加debugger, 他會自己reload

2008年8月7日 星期四

ruby xml html parser and Here Document

http://ihower.idv.tw/blog/archives/1666
http://en.wikipedia.org/wiki/Heredoc#Ruby
http://code.whytheluckystiff.net/hpricot/wiki/HpricotBasics
http://fcamel.twbbs.org/archives/2007/07/13/410/
http://www.rubyinside.com/ruby-xml-crisis-over-libxml-0-8-0-released-955.html
http://libxml.rubyforge.org/

[Here document]

print <<-EOB
abc
cde
#{1+1}
EOB



require 'rubygems'

require 'hpricot'
require 'open-uri'

url = "http://www.iwakela.com"

doc = Hpricot(open(url))
doc.search('table tr').each do |item|
(item/'a').each do |nav|
p nav.attributes['href']
p nav.inner_html
end
end


妳想parse的是xml 時妳有更好的選擇libxml

require 'pp'
require 'benchmark'
require 'rubygems'
require 'xml/libxml'
require 'hpricot'
require 'rexml/document'

include Benchmark

signal_keys = [:signal_type, :create_datetime, :realized_or_unrealized,
:close_datetime, :open_price, :close_price, :performance,
:annualized_performance]

$rsp1 = {}
$rsp2 = {}

bm(12) do |test|

str = File.read('test.xml')

test.report('libxml') do
#xml_doc = XML::Document.file('test.xml')
xml_doc = XML::Parser.string(str).parse
root = xml_doc.root
root.find('target').each do |t|
$rsp2[t.find_first('target_id').content] = rsp_target = {}
rsp_target[:target_performance_average] = t.find_first('performance_average').content
rsp_target[:target_annualized_performance_average] = t.find_first('annualized_performance_average').content

t.find('signal').each do |signal|
rsp_target[signal.find_first('signal_id').content.to_sym] = signal_keys.inject({}) do |h,k|
h[k] = signal.find_first(k.to_s).content ; h
end
end
end

end

test.report('hpricot') do
xml_doc = Hpricot(File.open('test.xml'))
(xml_doc/:target).each do |t|
$rsp1[t.at(:target_id).innerHTML] = rsp_target = {}
rsp_target[:target_performance_average] = t.at("performance_average").innerHTML
rsp_target[:target_annualized_performance_average] = t.at("annualized_performance_average").innerHTML
(t/:signal).each do |signal|
rsp_target[signal.at(:signal_id).innerHTML.to_sym] = signal_keys.inject({}) do |h,k|
h[k]=signal.at(k).innerHTML ; h
end
end
end
end

end


binding ruby

http://www.ruby-doc.org/core/classes/Kernel.html#M001057
http://www.ruby-doc.org/core/classes/Binding.html
http://onestepback.org/index.cgi/Tech/Ruby/RubyBindings.rdoc/style/print


def getBinding(str)
return binding
end
str = "hello"
eval "str + ' Fred'" #=> "hello Fred"
eval "str + ' Fred'", getBinding("bye") #=> "bye Fred"

class Demo
def initialize(n)
@secret = n
end
def getBinding
return binding()
end
end

k1 = Demo.new(99)
b1 = k1.getBinding
k2 = Demo.new(-3)
b2 = k2.getBinding

p eval("@secret", b1) #=> 99
p eval("@secret", b2) #=> -3
p eval("@secret") #=> nil

def getBinding(param)
return binding()
end
b = getBinding("hello")
p eval("param", b) #=> "hello"


2008年8月5日 星期二

redo, retry

http://my4java.itpub.net/post/9983/65275
http://www.souzz.net/big5.php?/html/345/23493.html

retry

例:

retry

語法:

retry

在迭代、塊或for語句中使用retry,意味著重啟迭代器。同時迭代器的參數也將被重新計算。

for i in 1..5
retry if some_condition # 從 i == 1 開始重新執行
end

2008年6月10日 星期二

model Object attribute attr_accessor

validate_of_ooxx :email 就只是去用email的mehod去get 而才不管他是table column還是attribute

Object.get_something 也是

所以最常見的就是
attr_accessor :plain_password
validates_presence_of :plain_password, :if => :password_required?
validates_presence_of :plain_password_confirmation, :if => :password_required?
validates_length_of :plain_password, :within => 4..40, :if => :password_required?
validates_confirmation_of :plain_password, :if => :password_required?

def password_required?
password.blank? || !plain_password.blank?
end


----
attr_accessor 可以幫妳做get set
想要overwrite
def plain_password
#this is get method
end

def plain_password=(ooxx)
#this is set method
end

----
>> User.find(856).plain_password
=> nil
>> User.find(856).adfafafd
NoMethodError: undefined method `adfafafd' for #<User:0xb75ac17c>
from /usr/local//lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1860:in `method_missing'
from (irb):13
>>

如果有這個method(attribute?!) 叫了沒東西會是 nil, 可是如果是沒這method(attribute?!)就直接有exception

2008年5月7日 星期三

ruby shoallow copy deep copy

http://zeljkofilipin.com/2006/02/09/ruby-deep-copy-object/
http://phpbb.godfat.org/viewtopic.php?p=1118&sid=db10ef9c975a1b57323311804559ed83
http://saaridev.wordpress.com/2008/01/09/ruby-deep-copy/
http://www.dotnetspider.com/forum/ViewForum.aspx?ForumId=2239



1. # problem
2. a = [[0, 1], [10], 19]
3. b = a.dup
4. b[0][1] = 100 #=> b = [[0, 100], [10], 19] and a = [[0, 100], [10], 19]
b[0] = [3,4] #=> b =[[3,4], [10], 19] and a = [[0, 100], [10], 19]

看起來ruby的 copy 不夠 deep

5.
6. # solution
7. a = [[0, 1], [10], 19]
8. b = Marshal.load(Marshal.dump(a))
9. b[0][1] = 100 #=> b = [[0, 100], [10], 19] and a = [[0, 1], [10], 19]