2013年6月25日火曜日

R2D

GUIをやるべくruby2.0から使えるR2Dというのを導入することに

https://github.com/takaokouji/r2d


$git clone https://github.com/takaokouji/r2d.git
まずgitで落としてきましょう。

$gem install r2d
これをやるとそれらしい返答がきますが、嘘らしいです。

gitで落としてきたら、そのフォルダに入ります。
$ cd r2d
Version '2.0.0' is to confusing to select ruby interpreter.
なんか怒られますが、そのまま無視で問題なかったです(私は)

$gem build r2d.gemspec
これを打ってbuildします。

  Successfully built RubyGem
  Name: r2d
  Version: 0.0.0
  File: r2d-0.0.0.gem

こんな返答だったら大丈夫のはず。

これでインストール開始!

$ gem install --local r2d-0.0.0.gem
ERROR:  While executing gem ... (Gem::DependencyError)
Unable to resolve dependencies: r2d requires gosu (~> 0.7.47.1)
UserName-no-MacBook-Air:r2d UserName$ port install gosu
Warning: port definitions are more than two weeks old, consider using selfupdate
Error: Insufficient privileges to write to MacPorts install prefix.

gosuがないと怒られているので、gemで入れます。

$ gem install gosuFetching: gosu-0.7.47.1.gem (100%)
Building native extensions.  This could take a while...
Successfully installed gosu-0.7.47.1
1 gem installed
Installing ri documentation for gosu-0.7.47.1...
unable to convert "\xCF" from ASCII-8BIT to UTF-8 for lib/gosu.bundle, skipping
Installing RDoc documentation for gosu-0.7.47.1...
unable to convert "\xCF" from ASCII-8BIT to UTF-8 for lib/gosu.bundle, skipping

gosuが入ったらgemでインストール

$ gem install --local r2d-0.0.0.gem
Successfully installed r2d-0.0.0
1 gem installed
Installing ri documentation for r2d-0.0.0...
Installing RDoc documentation for r2d-0.0.0...

これで導入おしまい!!!
exampleの中にサンプルがあるので、動かしてみましょう。





Ruby2.0.0を導入するまでのあれこれ2

前回はrvmにRuby2.0.0-p195を入れることまではできたが、
肝心なuseができなかった。そこのところを調べて修正
まあ、結局友達に聞いたわけですがorz

$ rvm use ruby-2.0.0-p195

RVM is not a function, selecting rubies with 'rvm use ...' will not work.

You need to change your terminal emulator preferences to allow login shell.
Sometimes it is required to use `/bin/bash --login` as the command.
Please visit https://rvm.io/integration/gnome-terminal/ for a example.

前回のエラーです。どうやらターミナルが最初に読み込むbashに問題あったようです

通常Macのターミナルではbashを最初に読み込み、そのタイミングでbashrcを読み込みます。
bashrcには
source "$HOME/.rvm/scripts/rvm"
というのを書く必要があったのですが、まずここでスペルミスをしていたという痛い事故が発生していました。これも大問題の一つだったわけですが、それ以上に問題がありました。bashがbashrcを読み込んでいなかったんです。なんでなのかようわからん。しらん。

そこでbashが読み込むbash_profileに以下の設定を追加しました。

# set rvm PATH
export PATH=$PATH:/Users/UserName/.rvm/bin/  #もともと書かなきゃいけない
source $HOME/.bashrc  #ここが重要

これでbash_profileが読み込まれるついでにbashrcがよみ込まれ、めでたくrvmが動くわけです。長かった・・・・

なんでbashがbashrcを読まなかったのかは、また今度暇な時に調べます。
いろいろ間違ってたら、ごめんなさい。

2013年6月22日土曜日

Ruby2.0.0を導入するまでのあれこれ

Ruby2.0を利用しなければならないけど
rvmの不具合を放置していたため詰んでる途中です
メモメモ

$ rvm install ruby-2.0.0-p195  →→エラー
$ less /Users/arakawahikari/.rvm/log/ruby-2.0.0/fetch.log を確認
$ rvm install ruby-2.0.0-p195 --verify-downloads 1 でうまくいくようだ

$ rvm use ruby-2.0.0-p195

RVM is not a function, selecting rubies with 'rvm use ...' will not work.

You need to change your terminal emulator preferences to allow login shell.
Sometimes it is required to use `/bin/bash --login` as the command.
Please visit https://rvm.io/integration/gnome-terminal/ for a example.

$ /bin/bash --login

$ rvm use ruby-2.0.0-p195

RVM is not a function, selecting rubies with 'rvm use ...' will not work.

You need to change your terminal emulator preferences to allow login shell.
Sometimes it is required to use `/bin/bash --login` as the command.
Please visit https://rvm.io/integration/gnome-terminal/ for a example.


とりあえず https://rvm.io/integration/gnome-terminal/ を確認する

 

2013年6月7日金曜日

CSVをMySQLに読み込ませる

できると便利だね、ということでやってみることに。
だが、カラムが多すぎていろいろとうまくいきませんでした。
インポート自体はできるので、あとは何かな、注意力のなさですかね^0^
mysql5です

> mysql5 --local-infile=1 -uUSERNAME -pPASSWORD TOURISM
まずはこれでログインします。
--local-infile=1 をつけておかないと、幸せになれません。

mysql>create database HOGE;
mysql>use HOGE;
mysql>create table HOGEHOGE(
           ・・・・・
           );
mysql> desc HOGEHOGE;  ちゃんとできてるか確認

データベースと、CSVのデータにあわせたテーブルを作ってやります。
お金を取り扱う場合、intではなくDECIMALがいいそうな。
型についてはあんま調べてません。昔の記憶を頼りに。

CSVをインポート
mysql> LOAD DATA LOCAL INFILE "CSVデータがある絶対パス"  INTO TABLE テーブル名 FIELDS TERMINATED BY ',';

これで挿入完了でございます。
なんか失敗したなって時は、テーブルを空にしましょう
mysql> truncate table 2008basic_ans;

--local-infile=1をつけてログインした理由は、LOAD DATA LOCAL INFILEを使うためです。

おしまい。

2013年6月6日木曜日

MySQLにログインできない時の対処



MySQLのセットアップを毎度忘れて毎度時間をかけてしまう。
前に入れて満足じまいだったので今回はメモする。
無論自分だけでは対処できなかった屑である

久々にSQL使う用事ができたため以下を実行。
>mysql5 -u root
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

こんなエラーが出てきてMySQLに入れない現象に陥った。
パスワードを設定は行っていなかったはず。

いろいろ調べた結果、とりあえずSQLは止めた方がよいと
>sudo /opt/local/share/mysql5/mysql/mysql.server stop

次に、以下のコマンドを実行した
>sudo mysqld_safe5 --user=root --skip-grant-tables
バックグラウンドで動かしたいときは & を語尾にくわえる

>mysql5 -u root
これで一応入れるようになる。mysqld_safe5で実行したのを止めよう。

ROOTユーザを追加したいため、とりあえず
>flush pgesrivile;
を実行する。これをしないとお前じゃ無理と怒られる。

grant all privileges on *.* to root@localhost identified by 'PASSWORD' with grant option;
これで追加できます。

>mysql5 -u root -pPASSWORD

おしまい。
参考になりました。
http://www.goofoo.jp/2011/11/1457

また、上記エラーが出ても
>mysql5
で入ることができる。
でもなんか、やっぱりパスワードあった方がいいよね、いろいろと。

ちなみにmysqld_safe5はCtrl+cで殺せなかった・・・・
>ps aux | grep mysqld_safe
とすると、端末閉じたとしても残っています。
>sudo kill -9 プロセスナンバー
 で殺しましょう。
 なんでなのかな、にわかなのでわかりません。

2013年1月16日水曜日

RubyでCSVをXMLに変換

あまり汎用性はないですが・・・
勉強がてらに書いてみました。
 某ペンを管理するツールを作ろうかと最近思い、とりあえず色の基本データをXMLに書き出すことにしました。
builder/xmlmarkupを使うと随分直感的な表現でXMLが書けます。感動した。
ruby 1.9で書いてます。


# encoding: utf-8
require 'builder/xmlmarkup'
require 'csv'
xm = Builder::XmlMarkup.new :indent => 2
xm.instruct! directive_tag=:xml, :encoding=> 'UTF-8'

print "変換するコピックデータを指定してください:"
readfile = gets.chop.to_s

f=CSV.read("./#{readfile}.csv")     #ファイルオープン
puts xm.root{
  xm.comment! "コピックのRGBデータ"
  xm.copic{
    f.each{|info|
      if /(^+[A-Z]{1,2})/ =~ info[0] or /¥d{1,3}/ =~ info[0] then
        xm.color("type"=>$&){
          xm.name(info[2])
          xm.number(info[0])
          xm.rgbcode(info[1])
        }
      end
    }
  } 
}
print "ファイルネームを指定してください:"
filename = gets.chop.to_s
File.open("#{filename}.xml","w"){|xml|
  xml << xm
}