2012年8月7日火曜日

rubyの小数点計算

小数点の計算と小数点以下N桁の表示にハマったのでメモしておきます。
roundは引数が持てるんですね。

1.9.3p194 :001 > 1/3
 => 0
1.9.3p194 :002 > 1/3.to_f
 => 0.3333333333333333
1.9.3p194 :003 > (1/3.to_f).round
 => 0
1.9.3p194 :004 > (1/3.to_f).round(2)
 => 0.33
1.9.3p194 :005 > 1/4
 => 0
1.9.3p194 :006 > 1.to_f/4
 => 0.25
1.9.3p194 :007 > (1.to_f/4).round
 => 0
1.9.3p194 :008 > (1.to_f/4).round(1)
 => 0.3
1.9.3p194 :009 > (1.to_f/4).round(2)
 => 0.25

2012年6月27日水曜日

railsでMySQLのデータが文字化け

RailsのActiveRecordからMySQLのデータ取得すると日本語が文字化け…。
MySQLのコンソールから見ると、ちゃんと見れる。
mysqldumpをすると文字化け…

default-character-setの指定が必要云々とあったので、
下記ファイルに
sudo /etc/mysql/my.cnf 
これを1行追加して
default-character-set=utf8
MySQL再起動すればOK!
$ sudo /etc/init.d/mysql restart


2012年5月2日水曜日

rvm の更新

今こんなものが入っていて、1.9.3を追加しようかと。
1.9.2だと、Redmine1.4を動かすのにプラグイン周りでエラーが出るのです…。

現状
$ rvm use list

rvm rubies

   jruby-1.6.2 [ amd64 ]
   ree-1.8.7-2011.03 [ x86_64 ]
   ruby-1.8.6-p420 [ x86_64 ]
   ruby-1.8.7-p352 [ x86_64 ]
   ruby-1.9.2-p290 [ x86_64 ]
rvm を最新に更新する 
しばらくやってないからか時間がかかる…
$ rvm get head
アップデートしたrvmの再読み込み
$ rvm reload
RVM reloaded!

インストール可能なRuby一覧
$ rvm list known
# MRI Rubies
[ruby-]1.8.6[-p420]
[ruby-]1.8.7[-p358]
[ruby-]1.8.7-head
[ruby-]1.9.1[-p431]
[ruby-]1.9.2-p180
[ruby-]1.9.2-p290
[ruby-]1.9.2-p318
[ruby-]1.9.2[-p320]
[ruby-]1.9.2-head
[ruby-]1.9.3-preview1
[ruby-]1.9.3-rc1
[ruby-]1.9.3-p0
[ruby-]1.9.3-p125
[ruby-]1.9.3-[p194]
[ruby-]1.9.3-head
ruby-head
…

1.9.3のインストール
$ rvm install 1.9.3
Installing Ruby from source to: /home/tiffany/.rvm/rubies/ruby-1.9.3-p194, this may take a while depending on your cpu(s)...
"--head"をつけるとリポジトリの最新リビジョン。
付けないとパッチがあたってる最新のものが取れるらしい。

使ってみる
$ rvm use 1.9.3
Using /home/tiffany/.rvm/gems/ruby-1.9.3-p194

1.9.2に切り替えてみる
$ rvm use 1.9.2
ruby-1.9.2-p320 is not installed.
To install do: 'rvm install ruby-1.9.2-p320'
あれ… どもう最新じゃないからインストールせよと…
とりあえず1.9.2もインストール。(こうやって更新するのね。ふむふむ)
$ rvm install ruby-1.9.2-p320

1.9.2に切り替え
$ rvm use 1.9.2
Using /home/tiffany/.rvm/gems/ruby-1.9.2-p320

もともとあった1.9.2には、こんなふうにすれば切り替わる。
$ rvm use ruby-1.9.2-p290
Using /home/tiffany/.rvm/gems/ruby-1.9.2-p290

2012年4月27日金曜日

MD5の作成

picasaにあるようなプライベート設定の認証キーにMD5を使う場合、
RubyからMD5キーを生成してみました。

irbで実行するとこんな感じ。
> require 'digest/md5'
=> true
> Digest::MD5.hexdigest("aa")
=> "4124bc0a9335c27f086f24ba207a4912"
> Digest::MD5.hexdigest("AA")
=> "3b98e2dffc6cb06a89dcb0d5c60a0206"

大文字小文字で違うキーが生成されます。

MD5のreverse用の辞書も存在するので、そこでマッチしない言葉を選びましょう!
http://md5.rednoize.com/

2012年4月20日金曜日

Sinatraをapacheで動かす

sinatraをapacheで動かしてみましょう。
Apacheが入ってることが前提で、passenger のインストールを行います。
先に作成した config.ru が必要になります。

下記を参考にしました!ありがとうございます!
http://d.hatena.ne.jp/foosin/20090619/1245426335

passengerのインストール
2つのモジュールが必要です。
1つ目はあっという間。
$ gem install passenger
Fetching: fastthread-1.0.7.gem (100%)
Building native extensions.  This could take a while...
Fetching: daemon_controller-1.0.0.gem (100%)
Fetching: passenger-3.0.12.gem (100%)
Successfully installed fastthread-1.0.7
Successfully installed daemon_controller-1.0.0
Successfully installed passenger-3.0.12
3 gems installed
Installing ri documentation for fastthread-1.0.7...
Installing ri documentation for daemon_controller-1.0.0...
Installing ri documentation for passenger-3.0.12...
Installing RDoc documentation for fastthread-1.0.7...
Installing RDoc documentation for daemon_controller-1.0.0...
Installing RDoc documentation for passenger-3.0.12...

2つ目はちょっと長いです。
$ passenger-install-apache2-module
Welcome to the Phusion Passenger Apache 2 module installer, v3.0.12.

This installer will guide you through the entire installation process. It
shouldn't take more than 3 minutes in total.

Here's what you can expect from the installation process:

 1. The Apache 2 module will be installed for you.
 2. You'll learn how to configure Apache.
 3. You'll learn how to deploy a Ruby on Rails application.

Don't worry if anything goes wrong. This installer will advise you on how to
solve any problems.

Press Enter to continue, or Ctrl-C to abort.
ここではとりあえずEnter押下。
しばし待つと
 1. The Apache 2 module will be installed for you.
が終わって、
 2. You'll learn how to configure Apache.

の説明が表示されます。

下記3行は環境によって異なりますので、メモしておきます。
あとで必要になります。
Load xxxxx
PassengerRoot xxxxx
PassengerRuby xxxxx

--------------------------------------------
The Apache 2 module was successfully installed.

Please edit your Apache configuration file, and add these lines:

   LoadModule passenger_module /home/tiffany/.rvm/gems/ruby-1.9.2-p290@rails3/gems/passenger-3.0.12/ext/apache2/mod_passenger.so
   PassengerRoot /home/tiffany/.rvm/gems/ruby-1.9.2-p290@rails3/gems/passenger-3.0.12
   PassengerRuby /home/tiffany/.rvm/wrappers/ruby-1.9.2-p290@rails3/ruby

After you restart Apache, you are ready to deploy any number of Ruby on Rails
applications on Apache, without any further Ruby on Rails-specific
configuration!

Press ENTER to continue.
無事インストールされたようなのでEnter押下。
最後に
 3. You'll learn how to deploy a Ruby on Rails application.

で、こういうメッセージが表示されます。
--------------------------------------------
Deploying a Ruby on Rails application: an example

Suppose you have a Rails application in /somewhere. Add a virtual host to your
Apache configuration file and set its DocumentRoot to /somewhere/public:

   <VirtualHost *:80>
      ServerName www.yourhost.com
      # !!! Be sure to point DocumentRoot to 'public'!
      DocumentRoot /somewhere/public   
      <Directory /somewhere/public>
         # This relaxes Apache security settings.
         AllowOverride all
         # MultiViews must be turned off.
         Options -MultiViews
      </Directory>
   </VirtualHost>

And that's it! You may also want to check the Users Guide for security and
optimization tips, troubleshooting and other useful information:

  /home/tiffany/.rvm/gems/ruby-1.9.2-p290@rails3/gems/passenger-3.0.12/doc/Users guide Apache.html

Enjoy Phusion Passenger, a product of Phusion (www.phusion.nl) :-)
http://www.modrails.com/

Phusion Passenger is a trademark of Hongli Lai & Ninh Bui.


Passengerのインストールはこれで終了です。


apacheの設定
環境によって異なると思いますが、、下記のファイルに上記3を参考に設定を行います。
$ sudo vi /etc/apache2/sites-available/xxx
そのファイルに下記を追加します。
上記でメモした3行

   LoadModule passenger_module /home/tiffany/.rvm/gems/ruby-1.9.2-p290@rails3/gems/passenger-3.0.12/ext/apache2/mod_passenger.so
   PassengerRoot /home/tiffany/.rvm/gems/ruby-1.9.2-p290@rails3/gems/passenger-3.0.12
   PassengerRuby /home/tiffany/.rvm/wrappers/ruby-1.9.2-p290@rails3/ruby
モジュールのパス設定
<VirtualHost *:80>
    ServerName www.yourhost.com
    DocumentRoot /home/tiffany/sinatra/public
    <Directory  /home/tiffany/sinatra/public>
        AllowOverride all
        Options -MultiViews
    </Directory>

     ErrorLog /var/log/apache2/error.log
     CustomLog /var/log/apache2/access.log combined
</VirtualHost>

これで、apacheを再起動すればOK!
RACK_ENV=productionでアプリが起動します。
もし、development環境でapache を動かしたい場合は、下記のようにRackEnv development をapacheの設定ファイルに追加してください。
    DocumentRoot /home/tiffany/sinatra/public
    RackEnv development