Ruby: Japanese OO script gem

[top]

  1. Object oriented script language.
  2. Very simple and regular syntax.
  3. Rich set of object oriented operation.
  4. Example: Cross referencer

    #!/usr/local/bin/ruby
    # tiny cross referencer written in Ruby
    #
    
    name = /[A-Za-z_][A-Za-z_0-9]*/
    xref = {}
    
    # pass 1
    while line = gets()
        line.scan(name) {|w|
    	if xref.key?(w) then
    	    xref[w].push($.) unless xref[w].include?($.)
    	else
    	    xref[w] = [$.]
    	end
        }
    end
    
    # obtain parameter for output
    maxlen = 0
    xref.each_key{|w| maxlen = w.length if w.length > maxlen }
    namefmt = "%#{maxlen}s:"
    lnofmt = "%#{Math.log10($.).ceil + 1}d"
    
    # pass2
    for w in xref.keys.sort!{|a, b| a.upcase <=> b.upcase} do
        printf namefmt, w
        xref[w].each{|lno| printf lnofmt, lno}
        print "\n"
    end
    

[top]