ROR Ruby(JRuby)小技巧


安装ruby on rails,ruby为JRuby ,version 为1.7.0.在使用gem install rails命名安装rails时一直遇到connection refused(ruby on rails gem install = Error fetching ,connection refused)问题,经过在网络上搜寻同样问题,找到解决方案如下:

gem install -p http://proxy_ip :proxy_port rails

附上原文地址http://stackoverflow.com/questions/4418/how-do-i-update-ruby-gems-from-behind-a-proxy-isa-ntlm

重上可以看出造成此原因是因为网络问题,所以可设置proxy来解决,在windows上可以如下设置:

步骤命令
进入windows控制台(运行->cmd)
输入命令 SET http_proxy = http://username:password@proxy_ip:port

rails&jruby 连接sql server问题

今天使用rails连接sql server2008遇到一个非常棘手的问题,害的我整整解决了近乎一天的时间,因为开始使用的是jruby1.7.0 ,以为是版本过高,于是换成了jruby1.6.8. 
最后解决了,发现这个和jruby的 ​​版本没有多大关系,这个问题应该不是每个人都能遇见,和自己的电脑环境有很大关系。写在这里供以后有同仁遇到少走弯路。

本机连接sql server数据库环境
1. activerecorder-jdbc-adapter (1.2.2.1) 
2. jruby1.6.8 
3. java 1.7.0_02 
4. database.yml配置

development:
  host: server
  adapter: jdbc
  username: user
  password: password
  driver: com.microsoft.sqlserver.jdbc.SQLServerDriver
  url: jdbc:sqlserver://server;databaseName=my_database
问题出现情况&原因使用范围解决方案
出现连接com.microsoft.sqlserver.jdbc.SQLServerDriver 驱动失败,提示: 驱动出现未知错误,加载com.microsoft.sqlserver.jdbc.SQLServerDriver 时找不到该驱动 是因为自己电脑没有安装驱动和环境设置不正确 电脑环境比较干净,没有安装其他的与java相关的程式,这个问题可能会因为安装有其他程式,程式安装时自动配置,就不会出现 1.下载jdbc驱动包sqljdbc_4.0.zip
2.解压后拷贝sqljdbc4.jar java包到当前使用java的jre下面的lib/ext下面
3.在系统环境变量增加CLASSPATH ,值为..\Java\jreX \lib\ext\sqljdbc4.jar(此值为你的java安装路径下面的lib\ext路径)

另一种方法是将sqljdbc4.jar拷贝到jruby的lib下面|

在一切都配置完毕以后,运行出现ActiveRecord::JDBCError , 
The driver encountered an unknown error: unable to choose type for timestamp from:"datetime2", "datetime"
这是activerecord-jdbc-adapter的一个bug,但是另一个同仁max未出现,我们两个的版本不同,可能是(1.2.2.1)版本的问题。 activerecorder-jdbc-adapter (1.2.2.1) 编辑lib\arjdbc\jdbc\type_converter.rb ,如下:
:timestamp    => [ lambda {|r| Jdbc::Types:: TIMESTAMP == r[ 'data_type' ].to_i},
                          lambda {|r| r[ 'type_name' ] =~ /^timestamp$/i},
                          lambda {|r| r[ 'type_name' ] =~ /^datetime$/i}, #新增此行
                          lambda {|r| r[ 'type_name' ] =~ /^datetime/i},
                          lambda {|r| r[ 'type_name' ] =~ /^date/i},
                          lambda {|r| r[ 'type_name' ] =~ /^integer/i}],  #Num of milliseconds for SQLite3 JDBC Driver
附上原贴地址jruby / activerecord-jdbc-adapter

WIN32OLE

JRuby

JRuby在1.6.X之后可以使用Win32OLE,可用下面方式设定

code
if defined ? RUBY_ENGINE && RUBY_ENGINE == 'jruby' 
    require 'jruby-win32ole'
else 
    require 'WIN32OLE'
end

控制Excel

Word的方式也一样

require 'WIN32OLE'
 
excel = WIN32OLE . new ( 'Excel.Application' )
excel.visible = true
 
workbook = excel.Workbooks.Add();
worksheet = workbook.Worksheets( 1 );
worksheet.Activate

截取Windows Process

if defined ? RUBY_ENGINE && RUBY_ENGINE == 'jruby'
    require 'jruby-win32ole'
else
    require 'WIN32OLE'
end
wmi = WIN32OLE .connect( "winmgmts://" )
processes = wmi.ExecQuery( "select * from win32_process" )
 
for process in processes do
        puts "Name: #{process.Name}"
        puts "CommandLine: #{process.CommandLine}"
        puts "CreationDate: #{process.CreationDate}"
        puts "WorkingSetSize: #{process.WorkingSetSize}"
        puts
end

LDAP

net-ldap

@ldap = Net:: LDAP . new
@ldap .host = "t1ldap.ad.garmin.com"
@ldap .port = 389
@ldap .auth self .login+ "@garmin" , password  # login, password
 
puts "@ldap: " + @ldap .get_operation_result.to_s

资讯交换平台和RichPOI内部系统有net-ldap和authlogic整合的code

rest-client

https://github.com/archiloque/rest-client

Simple HTTP and REST client for Ruby, inspired by microframework syntax for specifying actions.

response = RestClient.post " http://10.128.98.14:3001/user_sessions.json" , { :user_session => { :login => LOGIN , :password => PASSWORD }}.to_json, :content_type => :json , :accept => :json
 
puts "response.code: " +response.code.to_s
puts "response.to_str: " +response.to_s
@cookie = response.cookies
 
jdata = { :issue_record => { :comment => survey[ :comment ].to_s, :assign_user_id => _user.id, :end_timestamp =>survey[ :survey_done ]}}.to_json
res = RestClient.put ' http://10.128.98.14:3001/issue_records/' +survey[ :issue_record_id ].to_s+ '.json' , jdata, { :content_type => :json , :cookies => @cookie }
 
puts "res.code: " +res.code.to_s
puts "res.to_str: " +res.to_s
 
new_jdata = { :issue_record => { :issue_id => update_record.issue_id, :org_id => relate_record.org_id, :comment => "survey[#{survey[:issue_record_id]} complete!" , :user_id => _user.id , :assign_user_id => update_record.user_id, :priority => update_record.priority}}.to_json
new_res = RestClient.put ' http://10.128.98.14:3001/issue_records.json' , new_jdata, { :content_type => :json , :cookies => @cookie }
 
puts "new_res.code: " +new_res.code.to_s
puts "new_res.to_str: " +new_res.to_s

画图

image_voodoo(JRuby)

ImageVoodoo is an Image manipulation library with a ImageScience-compatible API for JRuby. Uses java.awt and javax.image APIs native to Java to perform image manipulation; no other dependencies needed.

ImageVoodoo.with_image( "c:\\original.jpg" ) do |img|
  img.thumbnail( 320 ) do |thumb|
    thumb.save "c:\\thumb.jpg"
  end
end

控制ImageMagick

About  ImageMagick

JRuby -  RMagick4J

Ruby -  RMagick

gem PLATFORM == 'java' ? 'rmagick4j' : 'rmagick'
require 'RMagick'

Graphviz

用程式画流程图,还可以画state_machine的图

Google

fusion-tables

for accessing Google Fusion Tables,Google Drive(Google Docs)的功能之一

@ft = GData::Client::FusionTables. new
r= @ft .clientlogin( "xxxxxx@gmail.com" , "xxxxxx" )
 
puts @ft .show_tables
 
@ft .show_tables.each { |_table|
  puts _table.id.to_s
  results = _table.select "*" , "WHERE ...."   # select, conditions
  results .each {|r|
    puts r[:item]
  }
}

Sinatra  -轻量化的web framework

可以用在提供简单功能的web service

ruby code
require 'sinatra'
 
get '/hi' do
 "Hello World!"
end
执行!
$ gem install sinatra
$ ruby​​ hi.rb
== Sinatra has taken the stage ...
>> Listening on 0.0.0.0:4567
posted @ 2013-04-25 12:21  重庆Debug  阅读(548)  评论(0编辑  收藏  举报