mysql,Linux,HighPerformance,ruby on Rails

2010年6月27日星期日

28字节ruby代码带来的乐趣!

是的。你没有看错,这是我今天看到的代码:
def p.method_missing*_;p;end

第一次看,还是没有看的全明白。然后咱们来个格式化后的版本:
class NilClass
def method_missing *_
nil
end
end

别钻牛脚尖,非要一个字符字符对,看中间精华部分。此代码的作用是针对实例对象类如@some_hash,是个hash,
你想得到key=>value值时,出现nil时,提供的解决办法。

你可能希望这样的代码:
if @some_hash[:some_key] # good looking version
# do something with @some_hash[:some_key]
end


但如果@some_hash是nil,上面的代码就会出exception了。
你不得不这么写:
if @some_hash && @some_hash[:some_key] # correct version
# do something with @some_hash[:some_key]
end

再来一个完整的代码实例:

# Egocentric nil
# code by Yohan, slightly edited and comments by me

# start ego mode block, usage:
# catch_nil do
# if @some_hash[:some_key].to_formatted_array[3]
# # do something
# end
# end
def catch_nil(&block)
# grip methods
ori_method_missing = NilClass.instance_method(:method_missing)
catch_method_missing = NilClass.instance_method(:catch_method_missing)
# activate ego mode
NilClass.send :define_method, :method_missing, catch_method_missing
# run code
yield
ensure
# no matter what happens: restore default nil behaviour
NilClass.send :define_method, :method_missing, ori_method_missing
end
#alias :egonil :catch_nil # ;)

# this is the ego nil
class NilClass
def catch_method_missing(m, *args, &block)
nil
end
end
不管怎么说,真的很优雅。ruby is awesome!

详细看这里:

The 28 Bytes of Ruby Joy!

--
tommy xiao
E-mail: xiaods(AT)gmail.com

2010年6月14日星期一

use jruby+sinatra+jfreechart build StackedBar graph! - Series 1

FYI,jruby is now 1.5.1, i want to build gpaph service.so i want to familiar with jruby and jfreechart api. this is blow doc is my practice notes.any feedback welcome.

first step:
use jruby+jetty+sinatra stack:


only git clone done.you will have done this stack in local

second step:

create your rb file:

charting.rb
==========
require 'rubygems'
require 'sinatra'
require 'java'

import 'org.jfree.data.category.DefaultCategoryDataset'


class DefaultCategoryDataset
  class Inner
    def initialize(dataset, row)
      @dataset, @row = dataset, row.to_s
    end

    # a value of nil can call 'removeValue'
    def []=(column, value)
      @dataset.addValue value.to_java(:int), @row, column.to_s
    end

    def populate(hash)
      hash.each_pair do |column, value|
        @dataset.addValue value.to_java(:int), @row, column.to_s
      end
    end
  end

  def [](row)
    Inner.new self, row
  end
end


module Graph
  class StackedBar
    include_class 'java.io.File'
    include_class 'org.jfree.chart.ChartUtilities'
    include_class 'org.jfree.chart.JFreeChart'
    include_class 'org.jfree.data.category.DefaultCategoryDataset'
    include_class 'org.jfree.chart.ChartFactory'
    include_class 'org.jfree.chart.plot.PlotOrientation'

    def initialize(width=600, height=400, data=[])
      @width = width
      @height = height
      dataset = create_sample_data() if data.empty?
      @chart = create_chart(dataset)
    end

    def render_to_file(filename, format="png")
      puts "Rendering graph to #{filename}"
      javafile = java.io.File.new(filename)
      ChartUtilities.saveChartAsPNG(javafile, @chart, @width, @height)
    end

    private
    def create_sample_data
      # This also allows symbols as well as strings
      dataset = DefaultCategoryDataset.new
      dataset[:Submitted].populate(:A => 1, :B => 2, :C => 3)
      dataset[:Assigned].populate(:A => 1, :C => 1)
      dataset["In-work"].populate(:A => 3, :C => 3)
      dataset[:InVerfication][:A] = 1
      dataset[:Delivered][:A] = 2
      dataset[:Rejected][:B] = 1
      dataset[:Closed][:B] = 1
      dataset["On-hold"][:C] = 2
      dataset
    end

    def create_chart(dataset)
      chart = ChartFactory.createStackedBarChart("XYZ's Development Projects",
                                                 "Project Name", 
                                                 "Hours", 
                                                 dataset, 
                                                 PlotOrientation::VERTICAL, 
                                                 true, 
                                                 true, 
                                                 false)
      return chart    
    end
  end # class StackedBar  
end # class Graph

get '/' do
  sb = Graph::StackedBar.new
  @pp = "stacked_bar.png"
  img_pub = "public/images/"
  sb.render_to_file(img_pub+@pp)
  erb :graph
end




--
tommy xiao
E-mail: xiaods(AT)gmail.com