--------------------- tom.luo$gmail.com 2010/3/6 --------------------- ri to Ruby is what perldoc to Perl. 1. Install ri on Ubuntu $sudo apt-get install ri 2. View the documentation for a given class $ri Array 3. View the documentation for a given method ri 'Array#delete' ----------------------------------------------------------- Array#delete array.delete(obj) -> obj or nil array.delete(obj) { block } -> obj or nil ------------------------------------------------------------------------ Deletes items from _self_ that are equal to _obj_. If the item is not found, returns +nil+. If the optional code block is given, returns the result of _block_ if the item is not found. a = [ "a", "b", "b", "b", "c" ] a.delete("b") #=> "b" a #=> ["a", "c"] a.delete("z") #=> nil a.delete("z") { "not found" } #=> "not found" Example: a=(1..100).to_a a.delete(88) ============ Arrary permutation =========== irb(main):012:0> a=(1..3).to_a => [1, 2, 3] irb(main):013:0> a.permutation.to_a => [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]] irb(main):014:0> (1..10).to_a.permutation.to_a.size => 3628800 irb(main):014:0> (1..10).to_a.inject(:*) => 3628800 10*9*8*7*6*5*4*3*2*1 = 3628800 ================ Quotes =============== You can include double quotes, single quotes and special characters in a BLOCK s = < [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] a.inject(:+) => 55 a.inject(:*) => 3268800 ================= Bignum ================= n=2**80 1208925819614629174706176 Bit reference returns the nth bit in the binary representation of n. n[0] is the least significant bit. n[80] = 1 irb(main):033:0> 80.downto(0) do |bit| print n[bit] end 100000000000000000000000000000000000000000000000000000000000000000000000000000000=> 80 =============== BigDecimal =============== require 'bigdecimal' require 'bigdecima/math' include BigMath pi = BigMath::PI(30) => # rb(main):019:0> pi.to_s => "0.314159265358979323846264338327950288419716939953046425865850678E1" =============== Converting number from decimal to hexadecimal, or octal to binary etc base conversion. =============== Decimal number to hexadecimal 16.to_s(16) => "10" 10.to_s(16) =>"a" (2^^27).to_s(16) "33b2e3c9fd0803ce8000000" "33b2e3c9fd0803ce8000000".to_i(16) ==> 1000000000000000000000000000 Hexadecimal to decimal "33b2e3c9fd0803ce8000000".to_i(16) ==> 1000000000000000000000000000 Decimal to binary 1024.to_s(2) ==> "10000000000" Binary to decimal "10000000000".to_i(2) ==> 1024 ============= Generate random strings ============= Math::E.step(Math::PI,0.000001) {|i| print i.to_s.crypt(‘tluo’), " "} ============== View ri doc in ANSI format ============== ri --format ansi -T Array.sort Format is to use when display output: ansi, bs, html, plain, simple To use ANSI, either also use the -T option, or tell your pager to allow control characters. ============== Install MySQL/Ruby on Ubuntu =============== http://oracleabc.com/ruby/mysql_ruby.txt ============== Read RSS news in VIM ============== http://oracleabc.com/ubuntu/read_news_in_vim.txt