Search
Categories
Ad

Posts Tagged ‘ruby’

mw: printing variables to Growl using Meow

Sunday, November 29th, 2009
mw

mw

Today I uploaded a new Rubygem called “mw” (pronounced as [mi'u]).

mw is a tiny useful library to output the result of pp to Growl.The original idea of Growl printing comes from jugyo’s g. Though the concept is great, g does not work on my environment. I’ve got “connection errors” from Growl.
I suspect that “ruby-growl” library on my machine does not work well.

Anyway, to overcome this problem, I reinvented the same feature using Meow, a Growl notification wrapper for the rb-cocoa environment.

INSTALL

First, you need to be a Mac-user, and have installed rb-cocoa.

port install rb-cocoa

Sinece I uploaded mw.gem to gemcutter.org, installing mw is as easy as

gem install gemcutter
gem tumble
gem install mw

USAGE

require 'mw'
mw "message"
mw YourClass.new

To change priority, you can use mw1~5. (mw3 is normal, mw1 is the lowest, and mw5 is the highest. mw4 and mw5 will fire the sticky notes, so that the note will live on your screen persitently.

begin
do_dangerous_and_raise_some_exception
rescue => e
mw4 e
end

SOURCES

all the sources can be seen at http://github.com/tomoya55/mw.

NOTES

1. If you are still not sure what mw is, please take a look at jugyo’s presentation of g.

2. My mw requires rb-cocoa and meow. If you runs ruby on Linux or Windows, you cannot use this library. use g instead.

root_url undefined

Friday, August 7th, 2009

Problem

On Ruby on Rails, you see the NameError of “undefined local variable or method `root_url’ for #<YourController>”.

Solution

You need to set map.root in config/routes.rb, such as

  mpa.root :controller => :welcome

Stop installing rubygems to your home directory when you don’t “sudo”

Thursday, August 6th, 2009

Problem

You don’t want to install rubygems under your home direcotry(~/.gem/ruby/1.x/) when you just forgot to “sudo” and don’t have right permissions to write to system directories.

Solution

First, You need to install rubygems newer than 1.3.2. To check the version, type as

gem -v
1.3.1

To update your gems itself, type as

sudo gem update --system

Next, you need to edit your ~/.gemrc file. Add

gem:  --no-user-install

at the bottom of the file.

Now, if you forgot to “sudo” when installing new rubygems, gem will simply fail with permission erros, like

tomoya@hongkong% gem install sequel
ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions into the /opt/local/lib/ruby/gems/1.8 directory.

Sending ISO-2022-JP Mail from Ruby1.8

Thursday, July 9th, 2009

Problem

You want to send Japanese mail encodded in ISO-2022-JP from Ruby

Solution

I wrote a small class called Hog for this purpose.

Fail to compile Ruby1.9 on VPS because of the RAM size.

Wednesday, June 17th, 2009

Problem

You want to compile ruby1.9, but failed with errors:

gcc -I. -I.ext/include/i686-linux -I./include -I. -D_FILE_OFFSET_BITS=64  -DONIG_ENC_REGISTER=rb_enc_register -fPIC  -O2 -g -Wall -Wno-parentheses  -o enc/trans/gb18030.o -c ./enc/trans/gb18030.c
virtual memory exhausted: Cannot allocate memory
make[1]: *** [enc/trans/gb18030.o] Error 1
make[1]: Leaving directory `/home/roiadmin/tmp/ruby-1.9.1-p129'
make: *** [trans] Error 2

You maybe tried changing ulimit values or add gcc-min-heapsize parameters, but all failed in vain.

Solution

The best way to do so is remove gb18030.c from compilation packages. Hope you are not planning to use Chinese GB18030 encoded-strings in Ruby.
The compile instructions are written in “enc.mk”. So all you need to do is delete all lines about gb18030 in “enc.mk”.

If you are compileing 1.9.1-p129, you can use this patch file.

*** enc.mk.org	Wed Jun 17 21:36:49 2009
--- enc.mk	Wed Jun 17 21:38:44 2009
***************
*** 79,85 ****
  	  enc/euc_kr.$(OBJEXT) \
  	  enc/euc_tw.$(OBJEXT) \
  	  enc/gb2312.$(OBJEXT) \
- 	  enc/gb18030.$(OBJEXT) \
  	  enc/gbk.$(OBJEXT) \
  	  enc/iso_8859_1.$(OBJEXT) \
  	  enc/iso_8859_2.$(OBJEXT) \
--- 79,84 ----
***************
*** 113,119 ****
  	 $(ENCSODIR)/euc_kr.$(DLEXT) \
  	 $(ENCSODIR)/euc_tw.$(DLEXT) \
  	 $(ENCSODIR)/gb2312.$(DLEXT) \
- 	 $(ENCSODIR)/gb18030.$(DLEXT) \
  	 $(ENCSODIR)/gbk.$(DLEXT) \
  	 $(ENCSODIR)/iso_8859_1.$(DLEXT) \
  	 $(ENCSODIR)/iso_8859_2.$(DLEXT) \
--- 112,117 ----
***************
*** 147,153 ****
  TRANSCSRCS = enc/trans/big5.c \
  	     enc/trans/chinese.c \
  	     enc/trans/escape.c \
- 	     enc/trans/gb18030.c \
  	     enc/trans/gbk.c \
  	     enc/trans/iso2022.c \
  	     enc/trans/japanese.c \
--- 145,150 ----
***************
*** 161,167 ****
  	    enc/trans/big5.$(OBJEXT) \
  	    enc/trans/chinese.$(OBJEXT) \
  	    enc/trans/escape.$(OBJEXT) \
- 	    enc/trans/gb18030.$(OBJEXT) \
  	    enc/trans/gbk.$(OBJEXT) \
  	    enc/trans/iso2022.$(OBJEXT) \
  	    enc/trans/japanese.$(OBJEXT) \
--- 158,163 ----
***************
*** 175,181 ****
  	   $(ENCSODIR)/trans/big5.$(DLEXT) \
  	   $(ENCSODIR)/trans/chinese.$(DLEXT) \
  	   $(ENCSODIR)/trans/escape.$(DLEXT) \
- 	   $(ENCSODIR)/trans/gb18030.$(DLEXT) \
  	   $(ENCSODIR)/trans/gbk.$(DLEXT) \
  	   $(ENCSODIR)/trans/iso2022.$(DLEXT) \
  	   $(ENCSODIR)/trans/japanese.$(DLEXT) \
--- 171,176 ----
***************
*** 267,276 ****
  	@$(MAKEDIRS) "$(@D)"
  	$(LDSHARED) -o $@ enc/gb2312.$(OBJEXT) $(LIBPATH) $(DLDFLAGS) $(LOCAL_LIBS) $(LIBS)
 
- $(ENCSODIR)/gb18030.$(DLEXT): enc/gb18030.$(OBJEXT)
- 	@$(MAKEDIRS) "$(@D)"
- 	$(LDSHARED) -o $@ enc/gb18030.$(OBJEXT) $(LIBPATH) $(DLDFLAGS) $(LOCAL_LIBS) $(LIBS)
- 
  $(ENCSODIR)/gbk.$(DLEXT): enc/gbk.$(OBJEXT)
  	@$(MAKEDIRS) "$(@D)"
  	$(LDSHARED) -o $@ enc/gbk.$(OBJEXT) $(LIBPATH) $(DLDFLAGS) $(LOCAL_LIBS) $(LIBS)
--- 262,267 ----
***************
*** 383,392 ****
  	@$(MAKEDIRS) "$(@D)"
  	$(LDSHARED) -o $@ enc/trans/escape.$(OBJEXT) $(LIBPATH) $(DLDFLAGS) $(LOCAL_LIBS) $(LIBS)
 
- $(ENCSODIR)/trans/gb18030.$(DLEXT): enc/trans/gb18030.$(OBJEXT)
- 	@$(MAKEDIRS) "$(@D)"
- 	$(LDSHARED) -o $@ enc/trans/gb18030.$(OBJEXT) $(LIBPATH) $(DLDFLAGS) $(LOCAL_LIBS) $(LIBS)
- 
  $(ENCSODIR)/trans/gbk.$(DLEXT): enc/trans/gbk.$(OBJEXT)
  	@$(MAKEDIRS) "$(@D)"
  	$(LDSHARED) -o $@ enc/trans/gbk.$(OBJEXT) $(LIBPATH) $(DLDFLAGS) $(LOCAL_LIBS) $(LIBS)
--- 374,379 ----
***************
*** 451,460 ****
  	-@$(MAKEDIRS) "$(@D)"
  	$(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $(encsrcdir)/gb2312.c
 
- enc/gb18030.$(OBJEXT): $(encsrcdir)/gb18030.c
- 	-@$(MAKEDIRS) "$(@D)"
- 	$(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $(encsrcdir)/gb18030.c
- 
  enc/gbk.$(OBJEXT): $(encsrcdir)/gbk.c
  	-@$(MAKEDIRS) "$(@D)"
  	$(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $(encsrcdir)/gbk.c
--- 438,443 ----
***************
*** 567,576 ****
  	-@$(MAKEDIRS) "$(@D)"
  	$(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $(encsrcdir)/trans/escape.c
 
- enc/trans/gb18030.$(OBJEXT): $(encsrcdir)/trans/gb18030.c
- 	-@$(MAKEDIRS) "$(@D)"
- 	$(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $(encsrcdir)/trans/gb18030.c
- 
  enc/trans/gbk.$(OBJEXT): $(encsrcdir)/trans/gbk.c
  	-@$(MAKEDIRS) "$(@D)"
  	$(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $(encsrcdir)/trans/gbk.c
--- 550,555 ----

Enjoy!

Ruby Sinatra application on Google App Engine Java

Wednesday, June 17th, 2009

This article is all about what I have done to make my first Sinatra application on Google’s App engine/Java aka GAE. As you know, Sinatra is a light web application framework for Ruby. We can run Sinatra on GAE because GAE has started to support Java since April 2009 and there existed a Java implementation of Ruby called JRuby.

Before proceeding, you are sure that you already have a valid account for GAE. Otherwise, hurry up to register your account at here.

OK, now you are ready and let’s get started!

Install JRuby

First, download a JRuby package using git. If you are unfamiliar with Git, tutorials and articles on github, including screencasts and podcasts are your good introduction.

Installation of git on Ubuntu is as easy as

% apt-get install git-core

If you are using other operating systems, check http://github.com/guides/home.

To install JRuby, type

% git clone git://kenai.com/jruby~main

Do not type jruby-main. jruby~main is correct.

Moving to jruby~main directory, start compiling.

% cd jruby~main
% ant
% ant jar-complete

Check the jruby version with

% bin/jruby –version
jruby 1.4.0dev (ruby 1.8.6p287) (2009-06-16 6586) (Java HotSpot(TM) Client VM 1.5.0_16) [i386-java]

Install Sinatra and related gems

Check the gem version with

% bin/jruby –S gem –version
1.3.3

To install rubygems for your new jruby, try

% bin/jruby –S gem install rake sinatra wabler mongrel
JRuby limited openssl loaded. gem install jruby-openssl for full support.
<a href="http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL">http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL</a>
Successfully installed rake-0.8.7
Successfully installed rack-1.0.0
Successfully installed sinatra-0.9.2
Successfully installed warbler-0.9.13
Successfully installed gem_plugin-0.2.3
Successfully installed mongrel-1.1.5-java
6 gems installed

Did you see OpenSSL messages, too? You can leave it for now.

Install JRuby-Rack

Get a copy of jruby-rack from github.

% cd ../ # only if you are still in jruby~main directory
% git clone git://github.com/nicksieger/jruby-rack.git
% cd jruby-rack
% ../jruby\~main/bin/jruby -S rake SKIP_SPECS=true

Be careful not to forget to add “SKIP_SPECS=true” when you perform rake command in jruby-rack. Without it, rake will fail.

Create Sinatra Appcalition

Now, let’s create a new Sinatra application. First make a directory for our first app.

% mkdir myway-app

Make directories

% mkdir views public config lib

Create files

1. config.ru

require 'rubygems'
require 'sinatra'
root_dir = File.dirname(__FILE__)
set :environment, :production
set :root, root_dir
set :app_file, File.join(root_dir, 'app.rb')
disable :run
require 'app'
run Sinatra::Application

2. app.rb

require 'rubygems'
require 'sinatra'
 
get '/' do
  "Hello from Sinatra running on Java!"
end

3. appengine-web.xml

<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
    <application>live-ch</application>
    <version>1</version>
    <static-files />
    <resource-files />
    <sessions-enabled>false</sessions-enabled>
    <system-properties>
      <property name="jruby.management.enabled" value="false" />
      <property name="os.arch" value="" />
      <property name="jruby.compile.mode" value="JIT"/> <!-- JIT|FORCE|OFF -->
      <property name="jruby.compile.fastest" value="true"/>
      <property name="jruby.compile.frameless" value="true"/>
      <property name="jruby.compile.positionless" value="true"/>
      <property name="jruby.compile.threadless" value="false"/>
      <property name="jruby.compile.fastops" value="false"/>
      <property name="jruby.compile.fastcase" value="false"/>
      <property name="jruby.compile.chainsize" value="500"/>
      <property name="jruby.compile.lazyHandles" value="false"/>
      <property name="jruby.compile.peephole" value="true"/>
   </system-properties>
</appengine-web-app>

4. config/warble.rb

Warbler::Config.new do |config|
  config.dirs = %w(lib views public)
  config.includes = FileList["appengine-web.xml", "app.rb"]
  config.staging_dir = 'war'
  config.java_libs = []
  config.gems = ['sinatra']
  config.gem_dependencies = true 
  config.webxml.booter = :rack 
end

For a test run,

% ruby app.rb
== Sinatra/0.9.2 has taken the stage on 4567 for development with backup from Mongrel

Go to http://localhost:4567/, and you will see a string message that sinatra is running. OK, press Ctrl+C to stop the server.

Setup for GAE

You need to download GAE SDK for Java from here. The latest version at this moment is version 1.2.1, released on May 13, 2009.

After the download is finished, we need to copy appengine-java-sdk-1.2.1/lib/user/appengine-api-1.0-sdk-1.2.1.jar to Sinatra Application’s lib directory.

% cp ~/Downloads/appengine-java-sdk-1.2.1/lib/user/appengine-api-1.0-sdk-1.2.1.jar lib/

Also, we need to copy jruby-rack jars to lib directory.

% cp ../jruby-rack/target/jruby-rack-0.9.5-SNAPSHOT.jar lib/

Next, we need to copy jruby itself in lib directory, and split it into a two files because the original jruby-complete.jar is too big to use in GAE.

% cp ../juby~main/lib/jruby-complete.jar lib/

Then, create a shellscript called split-jar.sh

#!/bin/sh
 
rm -rf jruby-core.jar
rm -rf ruby-stdlib.jar
rm -rf tmp_unpack
mkdir tmp_unpack
cd tmp_unpack
jar xf ../jruby-complete.jar
cd ..
mkdir jruby-core
mv tmp_unpack/org jruby-core/
mv tmp_unpack/com jruby-core/
mv tmp_unpack/jline jruby-core/
mv tmp_unpack/jay jruby-core/
mv tmp_unpack/jruby jruby-core/
cd jruby-core
jar cf ../jruby-core.jar .
cd ../tmp_unpack
jar cf ../ruby-stdlib.jar .
cd ..
rm -rf jruby-core
rm -rf tmp_unpack
rm -rf jruby-complete.jar

And run

% sh split-jar.sh

Try “ls” to see that you have “jruby-core.jar” and “jruby-stdlib.jar” instead of “jruby-complete.jar”.

Now it is time to create “war” files with warbler.

% ../jruby~main/bin/jrruby –S warble

Run at localhost.

% ../appgengine-java-sdk-1.2.1/bin/dev_appserver.sh --port=18080 war

If you have no error, open http://localhost:18080/ with your own browser. you will see the messages.

Deployment

You need to overwrite APPLICATION-ID with your own in appengine-web.xml. When finished, create war files again as

% ../jruby~main/bin/jrruby –S warble

Then running deployment script

% ../appgengine-java-sdk-1.2.1/bin/appcfg.sh update war

If you run this script first time, you will be prompted to input your email address and password. Input your email of your GAE account correctly. After a while, the script will be finished, and you can see your Sinatra/GAE apps at http://{YOUR-APPLICATION-ID}.appspot.com.

 

This is all. Enjoy making Ruby web application on GAE!

Dump YAML Strings with UTF-8 multibyte characters in Ruby

Tuesday, June 16th, 2009

Problem

You want to dump your Ruby object to YAML-formatted file, but your object contains UTF-8 multibyte characters. In this situation, original yaml dumper will output in a human-unfriendly format like

  - !binary |
   5a6X6LC35bKs

Solution

The easiest way to solve is to use ya2yaml.

 sudo gem install ya2yaml

Then, in your source code,

open("save.yml", "w") do |f| 
 f.write multiByteObj.ya2yaml
end

gisty for gist and gisty for Ruby1.9.1

Sunday, June 14th, 2009

Introduction

gist is a simple web service by github for posting and sharing your cool code snippets. gisty is a command-line client for gist.

Installation of gisty

sudo gem install nokogiri
sudo gem sources -a http://gems.github.com
sudo gem install swdyh-gisty

Usage

Posting a source code is as easy as

 gisty post hello.c world.c

Also, you can ‘clone’ all your code snippets.

 gisty sync

For more information, type

 gisty help





And the problem is…

Problem

gisty does not work with Ruby1.9.1.

Solution

I made a forked version of gisty on github repos. Soon, you will be able to download this version

sudo gem install tomoya55-gisty

Notice: you cannot install tomoya55-gisty at this moment. I am now investigating what is going on on my gems. Thank you.

REXML Error on Ruby1.9

Saturday, June 6th, 2009

Symptom

I encountered an REXML Error of

Last 80 unconsumed characters

when parsing utf-8 xml strings using REXML on Ruby1.9.1.

Solution

try check encoding

puts xmlstring.encoding #=&gt; 'US-ASCII' in my case

If the return value is not “utf-8″, you can change the encoding as below.

xmlstring.force_encoding("utf-8")

By this change, REXML will parse the input with the correct encodings.