Showing posts with label Jruby. Show all posts
Showing posts with label Jruby. Show all posts

Friday, May 30, 2008

json for ruby [jruby] install and usage

$> sudo gem install json
$> sudo jruby -S gem install json_pure

require 'rubygems'
require 'json'

class Range
def to_json(*a)
{
'json_class' => self.class.name,
'data' => [ first, last, exclude_end? ]
}.to_json(*a)
end

def self.json_create(o)
new(*o['data'])
end
end

puts (1..10).to_json
p JSON.parse((1..10).to_json)
puts JSON.parse((1..10).to_json) == (1..10)

json =<<-"JSON"
{
"hasItems": true,
"totalItems": 5,
"orderId": "xxx-xxxxx-xxx",
"hasDetails": true,
"details": [{"item": "2323-2323", "number": 2, "price": 2.00}, {"item": "2323-2324", "number": 3, "price": 3.00}]
}
JSON
p JSON.parse(json)["hasItems"] # => true
p JSON.parse(json)["totalItems"] # => 5

notice: keys and values in json string must be quoted.

Wednesday, May 21, 2008

ruby script/server lighttpd

在一个jruby on rails目录下面运行以下命令时报错:
$> ruby script/server lighttpd
.....
Couldn't find any pid file in '/Users/yu/Sites/RubyOnRails/tmp/pids' matching 'dispatch.[0-9]*.pid'
(also looked for processes matching "/Users/yu/Sites/RubyOnRails/public/dispatch.fcgi")
在lighttpd的log中看到如下信息:
2008-05-21 00:23:33: (log.c.75) server started
2008-05-21 00:23:33: (mod_fastcgi.c.1029) the fastcgi-backend /Users/yu/Sites/RubyOnRails/public/dispatch.fcgi failed to start:
2008-05-21 00:23:33: (mod_fastcgi.c.1033) child exited with status 9 /Users/yu/Sites/RubyOnRails/public/dispatch.fcgi
2008-05-21 00:23:33: (mod_fastcgi.c.1036) If you're trying to run PHP as a FastCGI backend, make sure you're using the FastCGI-enabled version.
You can find out if it is the right one by executing 'php -v' and it should display '(cgi-fcgi)' in the output, NOT '(cgi)' NOR '(cli)'.
For more information, check http://trac.lighttpd.net/trac/wiki/Docs%3AModFastCGI#preparing-php-as-a-fastcgi-programIf this is PHP on Gentoo, add 'fastcgi' to the USE flags.
2008-05-21 00:23:33: (mod_fastcgi.c.1340) [ERROR]: spawning fcgi failed.
2008-05-21 00:23:33: (server.c.908) Configuration of plugins failed. Going down.

经google后才发觉script/server lighttpd只能用于ruby on rails的项目上,通过以下命令:
$> /usr/local/lighttpd/bin/spawn-fcgi -f /Users/yu/Sites/RubyOnRails/public/dispatch.fcgi -p 12000 -s /tmp/rails-fcgi.socket
会得到成功的结果如下:
spawn-fcgi.c.197: child spawned successfully: PID: 2275
而在jruby的项目下调用spawn-fcgi则会报脚本错误。

另附dispatch.sh[reference: http://www.javaeye.com/topic/168989]


#!/bin/sh

DISPATCH_PATH=/Users/yu/Sites/cv2/public/dispatch.fcgi
SOCKET_PATH=//Users/yu/Sites/cv2/tmp/sockets
RAILS_ENV=developement
export RAILS_ENV

case "$1" in

start)
for num in 0 1 2
do
/Users/yu/Programs/lighttpd/bin/spawn-fcgi -f $DISPATCH_PATH -s $SOCKET_PATH/rails.socket-$num
done
;;

stop)
killall -9 dispatch.fcgi
;;

restart)
$0 stop
$0 start
;;

*)
echo "Usage: dispatch.sh {start|stop|restart}"
;;

esac

exit 0

Wednesday, November 14, 2007

Jruby on rails 安装中的问题

在安装好jdk和jruby后并测试jruby正常工作后,设置了几个命令别名,以防跟原来系统装的ruby命令冲突,ruby下的irb/rails/gem/ruby等命令是包含到系统中的。Jruby要以示区别:
$> vi .bashrc
alias jgem='/usr/local/jruby/bin/gem'
alias jruby='/usr/local/jruby/bin/jruby'
alias jrails='/usr/local/jruby/bin/rails'
alias jirb='/usr/local/jruby/bin/irb'
alias jrake='/usr/local/jruby/bin/rake'

重新登录后,jruby正常工作,但jgem报错,Google无果。只能命令行调jruby安装rails到Jruby环境下,-S cmd run the specified command in JRuby's bin dir。
$> jgem -v
/usr/bin/env: jruby -J-Xmx512M: No such file or directory
$> jruby -J-Xmx384m -S gem install rails --version 1.2.3 --include-dependencies
Bulk updating Gem source index for: http://gems.rubyforge.org
Successfully installed rails-1.2.3
Successfully installed activesupport-1.4.2
Successfully installed activerecord-1.15.3
Successfully installed actionpack-1.13.3
Successfully installed actionmailer-1.3.3
Successfully installed actionwebservice-1.2.3
Installing ri documentation for activesupport-1.4.2...
Installing ri documentation for activerecord-1.15.3...
....
新建Jrails项目如下:
$> jruby -S rails test
却不能用 $> jrails test
报错如下:
/usr/local/jruby/bin/rails: line 9: require: command not found
/usr/local/jruby/bin/rails: line 10: version: command not found
/usr/local/jruby/bin/rails: line 11: syntax error near unexpected token `('
/usr/local/jruby/bin/rails: line 11: `if ARGV.first =~ /^_(.*)_$/ and Gem::Version.correct? $1 then'
哪位仁兄如果看到此问题并有解决方案请跟贴指点,谢谢。
另附mongrel+jruby的windows安装过程:
$> jruby -S gem install mongrel --include-dependencies
$> jruby -S gem install gem_plugin
$> jruby -S gem install cgi_multipart_eof_fix
$> jruby -S gem install mongrel-1.1.1-jruby
ERROR: While executing gem ... (OpenURI::HTTPError)
404 Not Found
# Why can't install gem package from rubyforge? Make sure to invoke the command from the directory where the gem is downloaded.
$> wget http://rubyforge.org/frs/download.php/27884/mongrel-1.1.1-jruby.gem
$> jruby -S gem install mongrel-1.1.1-jruby
$> wget http://rubyforge.org/frs/download.php/20455/mongrel_jcluster-0.0.1.gem
$> jruby -S gem install mongrel_jcluster-0.0.1
# mongrel_cluster does not work with JRuby and mongrel_jcluster does not work on Windows. So Mongrel clusters cannot be configured on a Windows machine.
=================================================================================
$> wget http://rubyforge.org/frs/download.php/21765/hpricot-0.6-jruby.gem
$> jruby -S gem install hpricot-0.6-jruby
$> jruby -S gem install mechanize # not install scrubyt will raise error about openssl
$> jruby -S gem install scrubyt --include-dependencies # need install this gem package for using mechanize
$> wget http://rubyforge.org/frs/download.php/24515/ActiveRecord-JDBC-0.5.gem
$> jruby -S gem install ActiveRecord-JDBC-0.5
$> wget http://rubyforge.org/frs/download.php/23995/jruby-openssl-0.0.4.gem
$> jruby -S gem install jruby-openssl-0.0.4