ubuntu, ror, jQuery, css, website memo
太嫩, 沒啥心得, 用來紀錄每天學的
2008年12月30日 星期二
ruby memo
to_i(9)
gets
File.read("cc.txt")
fh = File.new("cc.txt", "w")
fh.puts f
fh.close
require just once
load will force reload
ruby -l demo.rb
ruby-1.6.8 -ve ...............
sudo gem updateh
sort_by
sort
-------------------------------------------------------------------------------
application layout no effect
apache config set the request go the where
Document /home/lake/rails_app/iwakela/public
public have a .htaccess will define which dispatch will be used
controller could use CGI and some data in form => use params method
session => use session method
玩弄YAML "string".to_yaml
YAML.load(
)
XML
JSON
lambda block,
sort_proc = lambda{|e| [e................]}
呼叫 &sort_proc
ruby -e 'p Dir["*.rb"]'
puts "ccc" #return nil
-------------------------------------------------------------------------------
Object.new.methods.sort
>> u.send(name)
NameError: undefined local variable or method `name' for #
from (irb):10
>> u.send(:name)
=> "早鳥"
>> u.send("name")
=> "早鳥"
>> u.respond_to?(name)
NameError: undefined local variable or method `name' for #
from (irb):12
>> u.respond_to?(:name)
=> true
>> u.respond_to?("name")
=> true
ruby's variable would not take the object's content directly , they just take the reference
new initialize
superclass
------------------------------
Singleton -> C -> M -> C -> M
super
------------------------------
instance method, self be known in run time
singleton in particulr object(could be class object, too)
class function is singleton function, too
self.x = C.x
singletion give us a chance to add new method in run time
some situation dot could not be ingored, self.venue = blahblah
$0 is the file name be executed
inner method could not take the outer scope variable
class C
aa = 1
def m
p aa
end
p aa
end
bareword, private method,
def aa
p 'aaa'
end
aa
,and puts, print are either
-----------------------------------
whem, ===
yield, block
[Q]: Why need code block
[A]: We could call the same function, but we could use different block to achieve different goal
yield, block both skills for using function like parameter
rescue Errno::ENOENT => e
-----------------------------------------
strip : lose the outer spaces
chop : remove last character
chomp : remove training newline
eql? is more like ==
equal? test two object is the same or not
:"Symbol need the quote if it has spaces"
--------------------------------------------
Array.new(3, "abc") would be all the same object
Array.new(3){"abc"} that would be different objects
add
unshift[.........]push
remove
shift[............]pull return the element be removed
[Q]: concat push, what is the different?
[A]: concat would sort, push would just add to the array tailer
replcae is change the content of the original object
>> [1,2,3].zip([4,5,6])
=> [[1, 4], [2, 5], [3, 6]]
>> [1,2,3].zip([4,5,6]).flatten
=> [1, 4, 2, 5, 3, 6]
>> [1,2,3].zip([4,5,6]).flatten.join
=> "142536"
>> [1,2,3].zip([4,5,6]).flatten.join(", ")
=> "1, 4, 2, 5, 3, 6"
>> [1,2,3].zip([6,2,3]).flatten.uniq
=> [1, 6, 2, 3]
-----------------------------------------------------
find
find_all (select)
include?
reject
>> User.find(:all, :conditions => "name like '%'").map(&:name)
=> ["早鳥", "flake", "flak", "lakela", "sandy", "lakela1", "什麼鬼什麼鬼什麼鬼啊啦", "lake.ilakela2", "lake+iwakela", "ilakelaaa"]
>> User.find(:all, :conditions => "name like 'lake'").map(&:name)
=> []
>> User.find(:all, :conditions => "name like 'lake%'").map(&:name)
=> ["lakela", "lakela1", "lake.ilakela2", "lake+iwakela"]
>> User.find(:all, :conditions => "name like 'lak%'").map(&:name)
=> ["lakela", "lakela1", "lake.ilakela2", "lake+iwakela"]
>> User.find(:all, :conditions => "name like '%lak%'").map(&:name)
=> ["flake", "flak", "lakela", "lakela1", "lake.ilakela2", "lake+iwakela", "ilakelaaa"]
>> User.find(:all).find_all{|u| /^lake/.match(u.name)}.map(&:name)
=> ["lakela", "lakela1", "lake.ilakela2", "lake+iwakela"]
>> User.find(:all).find_all{|u| /lake/.match(u.name)}.map(&:name)
=> ["flake", "lakela", "lakela1", "lake.ilakela2", "lake+iwakela", "ilakelaaa"]
>> h = {"a" => 1, "b" => 2, "c" => 3}
=> {"a"=>1, "b"=>2, "c"=>3}
>> h.values_at("a", "b")
=> [1, 2]
>> h["d"]
=> nil
>> h.fetch("d")
IndexError: key not found
from (irb):17:in `fetch'
from (irb):17
from :0
Hash.new{block} block would be executed when access key does not exist
>> h = Hash.new{|hash, key| hash[key] = key }
=> {}
>> h["lake"]
=> "lake"
>> h
=> {"lake"=>"lake"}
>> h
=> {"flake"=>"flake", "lake"=>"lake"}
>> h2
=> {"lake"=>"lakelake"}
>> h3 = h.merge(h2)
=> {"flake"=>"flake", "lake"=>"lakelake"}
>> h3.invert
=> {"lakelake"=>"lake", "flake"=>"flake"}
>> h3.replace(h3.invert)
=> {"flake"=>"flake", "lake"=>"lakelake"}
>> h3.clear
=> {}
>> h3
=> {}
>> h4 = h.merge(h2)
=> {"flake"=>"flake", "lake"=>"lakelake"}
>> h4.keys
=> ["flake", "lake"]
>> h4.values
=> ["flake", "lakelake"]
>> { 1 => "a", 2 => "b"}.find_all{|k, v| k%2 == 0}
=> [[2, "b"]]
become array
>> { 1 => "a", 2 => "b"}.map{|k, v| [k*2, v.upcase]}
=> [[2, "A"], [4, "B"]]
>> h.has_key?(1)
=> true
>> h.has_value?(1)
=> false
sort
<=>
>> User.find(:all).map(&:name).sort
=> ["flak", "flake", "ilakelaaa", "lake+iwakela", "lake.ilakela2", "lakela", "lakela1", "sandy", "什麼鬼什麼鬼 什麼鬼啊啦", "早鳥"]
>> User.find(:all).map(&:name).sort do |a,b|; a.size <=> b.size;end
=> ["flak", "flake", "sandy", "lakela", "早鳥", "lakela1", "ilakelaaa", "lake+iwakela", "lake.ilakela2", "什麼 鬼什麼鬼什麼鬼啊啦"]
We could overwrite the <=> by a block
>> User.find(:all).map(&:name).sort_by do |a|; a.size;end
=> ["flak", "flake", "sandy", "lakela", "早鳥", "lakela1", "ilakelaaa", "lake+iwakela", "lake.ilakela2", "什麼 鬼什麼鬼什麼鬼啊啦"]
-------------------------------------------
>> 'abc def' =~ /a/
=> 0
>> /a/ =~ 'abc def'
=> 0
>> /g/ =~ 'abc def'
=> nil
>> 'abc def' =~ /g/
=> nil
Regexp stay in left or right is the same
if match, it would return the location of string, or return nil
>> /a/.match('abc def')
=> #
>> ('abc def').match(/a/)
=> #
>> m = 'ABClake.ilakela@gmail.com'.match(/([_a-z0-9-]+)(\.[_a-z0-9-]+)*(\+[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/)
=> #
>> m[0]
=> "lake.ilakela@gmail.com"
>> m[1]
=> "lake"
>> m[2]
=> ".ilakela"
>> m.captures[0]
=> "lake"
>> m.captures[1]
=> ".ilakela"
$& match 到的 m[0]
$` match到 前面的string m.pre_match
$' match到 後面的string m.post_match
$1 $2 第1 ,2個() 裡面的東西 m[1], m[2]
.>> m.begin(1) the first capture begin at location
=> 3
>> m.end(1)
=> 7
>> m = 'abc!def!ika!'.match(/.+!/)
=> #
>> m[0]
=> "abc!def!ika!"
>> m = 'abc!def!ika!'.match(/.+?!/)
=> #
>> m[0]
=> "abc!"
add ? after +
>> m = 'abc!def!ikdda!'.match(/\w{4}+!/)
=> #
>> m[0]
>> m = 'abc!def!ikdda!'.match(/.{4,5}!/)
=> #
>> m[0]
=> "c!def!"
>> m = 'a3b4c5'.match(/([a-z]\d){2}/)
=> #
>> m[0]
=> "a3b4"
>> m = 'abc def. gga'.match(/[a-z]+(?=\.)/)
=> #
>> m[0]
=> "def"
>> m = 'abc def. gga'.match(/[a-z]+(?!\s)/)
=> #
>> m[0]
=> "ab"
>> "a,b;c#d".split(/,|;|#/)
=> ["a", "b", "c", "d"]
>> "a,b;c#d".split(/,|;|#/, 2)
=> ["a", "b;c#d"]
>> "a,b;c#d".split(/,|;|#/, 3)
=> ["a", "b", "c#d"]
>> "a,bd,gre,gef,a".sub(/\w{3}(?=,)/) {|s| s.upcase}
=> "a,bd,GRE,gef,a"
>> "a,bd,greGEF,a".sub(/(\w{3})([A-Z]{3})/, '\2\1')
=> "a,bd,GEFgre,a"
>> ["ab", "bcd", "efg"].grep(/\w{3}/) {|w| w.upcase}
=> ["BCD", "EFG"]
>> ["ab", "bcd", "efg"].select {|w| p w.upcase if w.match(/\w{3}/) }
"BCD"
"EFG"
=> []
>> ["ab", "bcd", "efg"].select {|w| w.upcase if w.match(/\w{3}/) }
=> ["bcd", "efg"]
>> ["ab", "bcd", "efg"].map {|w| w.upcase if w.match(/\w{3}/) }
=> [nil, "BCD", "EFG"]
>> ["1","2",3].grep(/\d/)
=> ["1", "2"]
--------------------------------
Proc.new is different from lambde in return scope
const_set
alias_method :old_destroy, :destroy
def destroy(r)
if r
old_destroy
else
update_attribute(:removed_at, Time.now) if !removed_at
end
end
----------------------------------
class Work < ActiveRecord::Base
belongs_to :composer
def composer
Composer.find(composer_id)
end
end
Returns a copy of self with all nil elements removed.
[ "a", nil, "b", nil, "c", nil ].compact
#=> [ "a", "b", "c" ]
-------------------------------------
Association callbacks, before_add, after_add, before_remove and after_remove
module FindOrCreateByNameExtension
def find_or_create_by_name(name)
first_name, last_name = name.split(" ", 2)
find_or_create_by_first_name_and_last_name(first_name, last_name)
end
end
class Account < ActiveRecord::Base
has_many :people, :extend => FindOrCreateByNameExtension
end
class Company < ActiveRecord::Base
has_many :people, :extend => FindOrCreateByNameExtension
end
class Account < ActiveRecord::Base
has_many :people, :extend => [FindOrCreateByNameExtension, FindRecentExtension]
end
Post.find(:all, :include => [ :author, :comments ])
Post.find(:all, :include => [ :author, { :comments => { :author => :gravatar } } ])
Post.find(:all, :include => [ :author, :comments ], :conditions => ['comments.approved = ?', true])
沒有留言:
張貼留言
較新的文章
較舊的文章
首頁
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言