mysql,Linux,HighPerformance,ruby on Rails

2010年7月24日星期六

Rails3beta4使用Ajax的一些参考

update: prototype更新有点误解,现在已经更正。

Rails3Beta4的Ajax参考文档目前不是很多,因为版本更新很快,参考的内容很多都已经过期.完整可参考的文档少之又少.经过近期项目的经验,我想总结一下:
1, Rails3Beta4已经快接进RC,想使用ajax,直接在link_to,form_tag,form_for加上remote=> true,就能产生ajax request, Controller里加不加respond_to都会去找rjs,或js.erb.两个后缀没有什么差别,关键在于使用js.erb感觉和.html.erb统一了后缀名而以.(今天跑个demo,才知道使用js.rjs就可以调用prototype了,方法可以在ActionView::Helpers::PrototypeHelper::JavaScriptGenerator::GeneratorMethods里找到。你可以使用railsapi.com生成的文档查看到 )
2.我在使用form_tag里加上select_tag,想使用select_tag的onchange加上submit事件来请求form_tag中的ajax,不能成功.感觉是我的思路的问题,直接使用select_tag没有remote属性,不过直接使用ujs的onchange事件里加上ajax就能控制.通过这样的体会,你会发现使用UJS确实比Rails之前的RJS更简单,更方便,实现的效果和原来的是一样的.一样可以调用contorller里的方法及js.erb.
JQuery的UJS调用参考文档多,所以大家才想办法在Rails里去掉prototype使用jquery.不然如果直接prototype,怎么使用还需要去看prototype,有点让新人不知手措.(教程是没更新的,但查文档是有helper函数,生成项目试用就可以上手。)
总结,Rails3里,ajax的使用更开放,也就是说,其实你在rails3中使用哪个框架都可以,只需要修改一个rails.js就可以了.
参考:

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

2010年7月16日星期五

RVM,标准配置Ruby环境的首选

rvm,快速,高效,简洁,所以你需要的优化,它全部具备。只要你有一台Linux/Mac系统,就可以在5分钟内完成安装(限制仅在你的网速)。
第一步:
$ bash < <( curl http://rvm.beginrescueend.com/releases/rvm-install-head )

第二步:
在你的.bash_profile,把这段放在最后一行,保证PATH里有它就可以.贴上后,新开一个terminal,然后source ~/.bash_profile就可以了。

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
第三步:
安装你可爱的Ruby环境吧
$ rvm install 1.8.7
可选,第四步:
想使用就这样操作:
 $ rvm 1.8.7

想知道更多,运行这句:

$ rvm list
或者:
$ rvm help


第五步:
想把整个ruby版本作为自己默认的环境,运行这句:
$ rvm 1.8.7 --default

收工。去玩Ruby& rails吧

参考这里:
http://everydayrails.com/2010/06/28/rvm-gemsets-rails3.html
--
tommy xiao
E-mail: xiaods(AT)gmail.com

2010年7月11日星期日

ruby1.9.1对String的处理有细微差别,注意

今天在ruby1.9.2-dev环境下起Oria,报了个错:
/Users/xiaodeshi/.rvm/gems/ruby-1.9.2-head@rails3beta/gems/oria-0.1.1/lib/oria.rb:57:in `+': String can't be coerced into Fixnum (TypeError)
        from /Users/xiaodeshi/.rvm/gems/ruby-1.9.2-head@rails3beta/gems/oria-0.1.1/lib/oria.rb:57:in `block in port'
        from /Users/xiaodeshi/.rvm/gems/ruby-1.9.2-head@rails3beta/gems/oria-0.1.1/lib/oria.rb:57:in `each'
        from /Users/xiaodeshi/.rvm/gems/ruby-1.9.2-head@rails3beta/gems/oria-0.1.1/lib/oria.rb:57:in `inject'
        from /Users/xiaodeshi/.rvm/gems/ruby-1.9.2-head@rails3beta/gems/oria-0.1.1/lib/oria.rb:57:in `port'
        from /Users/xiaodeshi/.rvm/gems/ruby-1.9.2-head@rails3beta/gems/oria-0.1.1/bin/oria:43:in `parse_options'
        from /Users/xiaodeshi/.rvm/gems/ruby-1.9.2-head@rails3beta/gems/oria-0.1.1/bin/oria:49:in `start'
        from /Users/xiaodeshi/.rvm/gems/ruby-1.9.2-head@rails3beta/gems/oria-0.1.1/bin/oria:11:in `go'
        from /Users/xiaodeshi/.rvm/gems/ruby-1.9.2-head@rails3beta/gems/oria-0.1.1/bin/oria:76:in `<top (required)>'
        from /Users/xiaodeshi/.rvm/gems/ruby-1.9.2-head@rails3beta/bin/oria:19:in `load'
        from /Users/xiaodeshi/.rvm/gems/ruby-1.9.2-head@rails3beta/bin/oria:19:in `<main>'

心中奇怪,这昨天才用了一下没这问题啊。找开对应文件定位到:
  def self.port
    @@port ||= "Oria is easily the coolest in-memory, super-simple KVS. Cool people use it.".split("").inject(0) {|total, char| total += (char[0].class == String ? char[0].ord : char[0])}
  end

直接去irb下跑,原来作者是使用charcode加出来的一个端口,但1.9.2修正了1.8.7字符串的显示不一致问题。
原来在1.8.7下,'a'[0]出来的是ascii code,但在1.9.2下'a'[0]还是返回'a',这代码就跑不动了。
ok,了解原因后,改一行就ok了。
@@port ||= "Oria is easily the coolest in-memory, super-simple KVS. Cool people use it.".split("").inject(0) {|total, char|  total += (char[0].class == String ? char[0].ord : char[0])}

兼容1.8.7.


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

mac下删除ports,使用homebrew

homebrew是为rubist准备的安装管理器,所以port删除是当务之急。
search并当了一回小白鼠,成功! http://guide.macports.org/chunked/installing.macports.uninstalling.html
第一步:
% sudo port -f uninstall installed
第二步:
% sudo rm -rf \
/opt/local \
/Applications/DarwinPorts \
/Applications/MacPorts \
/Library/LaunchDaemons/org.macports.* \
/Library/Receipts/DarwinPorts*.pkg \
/Library/Receipts/MacPorts*.pkg \
/Library/StartupItems/DarwinPortsStartup \
/Library/Tcl/darwinports1.0 \
/Library/Tcl/macports1.0 \
~/.macports


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

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

2010年5月18日星期二

CGI和FASTCGI协议认知心得

前段时间去面试,被问到关于什么是CGI协议,懵了不会。因为使用ruby总需要与CGI打交道,我还是今天把功课补上,毕竟是手上的事。读了一下CGI的RFC3875,如果用一句来说,它是一个纯文本的协议。
通过Meta关键字=VALUE形式组成一个数组,与上端的Web
Server和下连的Script互相交流。从我们web程序员的角度来说,是透明的。但它针对每个Request都会起一个进程,所以之后引进了FastCGI,改良的地方就是针对客户端的Request,FCGI只开一个主进程,内部使用分组复用的技术(有点像CDMA时分复用的技术,如果你有通信方面的背景的话,对这个肯定能有所了解。)加快处理并返回Response。
另外,RFC里有一个表达式:巴科斯范式,也顺便学了一下,你也不妨学一招吧.

参考:
http://www.ietf.org/rfc/rfc3875.txt
http://en.wikipedia.org/wiki/FastCGI