Saturday, May 24, 2008

tree implement in ruby

Just for fun, but it work well.

01 #!/usr/bin/env ruby
02 #
03 #
04
05
06 def tree(dirname, indent = " |")
07 puts dirname + "/" if indent == " |"
08 dir = Dir.open dirname
09 for f in dir
10 if f == "." || f == ".."
11 next
12 elsif File.directory? dirname+"/"+f
13 puts indent + "-" + f + "/"
14 tree( dirname+"/"+f, indent+" |")
15 else
16 puts indent + "-" + f
17 end
18 end
19 end
20
21 tree (ARGV[0]? ARGV[0] : ".")

 

Friday, May 23, 2008

closure tip

closure is so fun.
well design for meta programming.

note 1:

01 #!/usr/bin/env ruby
02
03 class Hash
04 def find_all
05 temp_hash = {}
06 each {|key,value| temp_hash[key] = value if yield(key,value) }
07 temp_hash
08 end
09 end
10
11 square = { 0 => 0, 1 => 1, 2 => 4, 3 => 9 }
12
13 new = square.find_all { |key,value| key > 1 }
14 puts new.inspect

pass yield values.

note 2:

01 #!/usr/bin/env ruby
02
03 class Person
04 attr_accessor :name
05 def initialize(name)
06 @name = name
07 end
08 end
09
10 def command(obj,method)
11 obj.each {|x| yield x.send(method)}
12 end
13
14 family = []
15
16 %w(shelling sherry appollo).each do |name|
17 family.push Person.new(name)
18 end
19
20 command family, :name do |x|
21 puts "hello, I am #{x}."
22 end

Every object in array call the same function.

 
 

Tuesday, May 20, 2008

Mac OS X Terminal tip

leopard terminal use xterm-color as default, but conflict with linux when ssh. The backspace act as delete(kill word at right not left). Mac OS hint provide a good solution, Copy /usr/share/terminfo/78/xterm-color on mac to ~/.terminfo/x/ on linux box. Not affect linux default setting. It saves my emacs in screen environment :D

 
 

Saturday, May 17, 2008

sample scheme

hello, world! here
#!/usr/bin/guile -s
!#
(write "hello, world!")


define function
(define square (lambda (n) (* n n) ) )
(write (square 3) ) ; output 9


link1: The Scheme Programming Language 3/e
link2: Scheme Tutorial
 
 

Tuesday, May 13, 2008

shelling.idv.tw

買了 shelling.idv.tw,不過什麼都還沒配置 :D

Monday, May 12, 2008

self.pm

some convenient usage I prefer. :p
package PackageName;
use self;
use strict;

my @args;

sub new {
@args = args;
for (@args) {
blah~!
}
bless {}, self;
}

1;

self.pm++
 
 

some vim

something is fun from learning the vi editor 6/e:
    dwelp change two words
xp change two alphabets

Sunday, May 4, 2008

內灣出遊微記

昨日帶友人繞過大半個新竹,一直以來都不喜新竹風物,常感食衣住行皆不盡人意,即便與大學同學出遊也甚感無聊,但昨日與友人暢遊內灣,吃盡美食,甚感愉快。看來在一個地方生活是否能讓人滿意,最重要的仍然是人。