日々の記録と、気がついたときにコードとか ~ a note of notions about anything
Mysql2::Error: Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation ‘=’: SELECT `comments`.* FROM `comments` WHERE (category = ‘utf8的ななにか’)
というエラーがでた。
どうやら文字コードが混じっているようす。よくいままでこのエラーが出なかったものだ、と思いつつしらべてみる。
mysql> show variables like ‘%character%’;
+————————–+—————————-+
| Variable_name | Value |
+————————–+—————————-+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | atin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+————————–+—————————-+
8 rows in set (0.00 sec)
え、まだutf8にしてなかったのか、と焦りつつ、「/etc/mysql/my.conf」を修正。
[client] default-character-set=utf8 #追加 [mysqld] default-character-set=utf8 #追加 character-set-server=utf8 #追加
「sudo /etc/init.d/mysql restart」して、確認するとutf8になっている。
とは言え、これではエラーが出つづける。どうやらInnoDBのエンコードが「latin1_swedish_ci」だった。
ということでmigrationファイルを修正。(railsの場合)
def self.up
create_table(:table_names ,:options =>'ENGINE=InnoDB DEFAULT CHARSET=utf8') do |t|
t.string :name
t.text :description
t.string :category
t.integer :status
t.timestamps
end
end
で、migrateしなおし、再度実行するとうまいこといった。
1.インスタンス作成、必要なパッケージの導入
2.phpMyAdminにインストール、データベース作成
3.WordPressのインストール
まずはインスタンス作成。ポート80(HTTP),3306(MySQL)は開放しておく。
ec2-serverにログイン。rootに切りかえ、パスワードを設定。
サーバの時間を日本に設定。
必要なパッケージをインストール。
これはなくてもよさそうだが、便利だと聞いたので念のため。
デフォルトのyumには入ってないので、rpm-forgeのレポジトリからインストール。
とりあえず最新のrpm
そして、phpmyadmin.confを以下のように書き換え。
# Order Deny,Allow
Allow from all
# Allow from 127.0.0.1
Alias /phpmyadmin /usr/share/phpmyadmin
Alias /phpMyAdmin /usr/share/phpmyadmin
Alias /mysqladmin /usr/share/phpmyadmin
————————————————————————————
http経由でアクセスできるよう設定。
サーバを動かす。
ec2-xxx-xxx-xx-xx.ap-northeast-1.compute.amazonaws.com/phpmyadminにアクセスして、データベースを作成。
mysqlにログインして、create database dbname;でもいい。
‘DB_NAME’, ‘DB_USER’, ‘DB_PASSWORD’を設定。
で、apacheと、すべての人に権限を与える。
これで、ec2-xxx-xxx-xx-xx.ap-northeast-1.compute.amazonaws.com/wordpressへ行くと、wordpressの設定画面になる。
2日目。1日目のほうが難しかった印象。
foldlや、遅延評価など。
あらためて見るとfoldlもfoldrもつかってないな。1問くらい利用すればよかった。この中でいうと、きっとQ3で使うべきだったんだろう。
Q1. Write a sort that takes a list and returns a sorted list.
qsort :: [Integer] -> [Integer]
qsort [] = []
qsort (x:xs) = (qsort smaller) ++ [x] ++ (qsort bigger)
where
smaller = [a| a <- xs ,a <= x]
bigger = [b| b <- xs, b > x]
Q2. Write a sort that takes a list and a function that compares its two arguments and then returns a sorted list.
バブルソートのことかな。
haskellでのバブルソートはめんどいので省略。
Q3. Write a Haskell function to convert a string to a number. The string should be in the form of $2,345,678.99 and can possibly have leading zeros.
toNumber :: [Char] -> Float
toNumber xs = read (parseStr xs) ::Float
parseStr :: String -> String
parseStr [] = []
parseStr (x:xs) | (x == '$' || x == ',') = parseStr xs
| otherwise = [x] ++ (parseStr xs)
Q4. Write a function that takes an argument x and returns a lazy sequence that has every third number, starting with x. Then, write a function that includes every fifth number, beginning with y. Combine these functions through composition to return every eighth number, beginning with x + y.
every3 x = x:(every3 (x + 3)) every5 x = x:(every5 (x + 5)) every8 x y = [a + b | (a,b) <- (zip (every3 x) (every5 y))]
Q5. Use a partially applied function to define a function that will return half of a number and another that will append \n to the end of any string.
divi x y = y / x half = divi 2 putsString x y = y ++ x puts = putsString "\n"
ずいぶんやってなかったんだけど、最近haskell勉強しはじめたので、つづきをやっていく。
英語版なので、問題文の意味をとりちがえてる部分もあるかもしれない。
問題1.How many different ways can you find to write allEven?
ぱっと考えて4つ。がんばればfoldlや、モナドつかってもできそうだけど、気
力なかった。
allEven1 :: [Integer] -> [Integer] allEven1 [] = [] allEven1 (x:xs) = if even x then x:allEven1 xs else allEven1 xs allEven2 :: [Integer] -> [Integer] allEven2 xs = [x| x <- xs, even x] allEven3 :: [Integer] -> [Integer] allEven3 xs = foldr (\ y ys -> if even y then (y:ys) else ys ) [] xs allEven4 :: [Integer] -> [Integer] allEven4 xs = filter even xs
問題2. Write a function that takes a list and reverses the same list
in reverse.
これは1種類でいいようだけど、いちおう2つ。
reverse1 :: [a] -> [a] reverse1 [] = [] reverse1 (x:xs) = reverse1 xs ++ [x] reverse2 :: [a] -> [a] reverse2 xs = foldl (\ ys y -> y:ys) [] xs
問題3. Write a function that builds two-tuples with all possible combina-tions of two of the colors black, white, blue, yellow, and red. Note that you should include only one of (black, blue) and (blue, black).
makeCombinations :: [String] -> [(String,String)] makeCombinations xs = [(x,y)| x <- xs, y <- xs, (x < y)]
問題4.Write a list comprehension to build a childhood multiplication table. The table would be a list of three-tuples where the first two are integers from 1-12 and the third is the product of the first two.
ふふ、よゆうだ。
makeTable :: [(Integer,Integer,Integer)] makeTable = [(a,b,a*b)| a <- [1..12], b <- [1..12], (a < b)]
問題5.Solve the map-coloring problem (Section 4.2, Map Coloring, on page 101) using Haskell.
えっ、むずかしいんすけど。1日目でこのレベルか。
ずいぶん冗長な表現になったので、もっといい解答あればおしえてください。
bannedCombination = [("Mississippi", "Tennessee"),
("Mississippi", "Alabama"),
("Alabama", "Tennessee"),
("Alabama", "Mississippi"),
("Alabama", "Georgia"),
("Alabama", "Florida"),
("Georgia", "Florida"),
("Georgia", "Tennessee")]
mapNames = ["Tennessee","Florida","Mississippi","Georgia","Alabama"]
colorNames = ["red","blue","green"]
-- 地名の数まで、[色,色...]の組み合わせをつくる。
colorList = rcomb colorNames (length mapNames)
rcomb :: [String] -> Int -> [[String]]
rcomb [] _ = []
rcomb xs 0 = [[]]
rcomb xs 1 = [[x] | x <- xs]
rcomb xxs@(x:xs) n = map (x:) (rcomb xxs (n-1)) ++ rcomb xs n
-- zipをつかって、[(地名,色)] の組み合わせをつくる。
mapColoringCombination :: [String] -> [[String]] -> [[(String,String)]]
mapColoringCombination map colors = [zip map c|c<- colors]
allMapColoringCombinations = mapColoringCombination mapNames colorList
-- 上でつくった組み合わせをふるいにかける。
allowCombination :: [(String,String)] -> [[(String,String)]] -> [[(String,String)]]
allowCombination xs ys = [y|y<-ys,scan xs y]
findValue :: Eq a => a -> [(a,b)] -> [b]
findValue key list = [value|(key',value) <- list,key == key']
scan :: [(String,String)] -> [(String,String)] -> Bool
scan [] ys = True
scan ((a,b):xs) ys | (findValue a ys) == (findValue b ys) = False
| otherwise = scan xs ys
allows = allowCombination bannedCombination allMapColoringCombinations
以前このような記事を書いていた。追加すべき知識を追加する。
●idをbigintにするためには
方法1 カラムの型を指定する
def self.up change_column :users, :id, :int, :limit=>20 end
ただし、こうするとauto_incrementが抜ける。
方法2 SQL文を直打ち
def self.up execute "ALTER TABLE hoges MODIFY id bigint(20) DEFAULT NULL auto_increment NOT NULL;" end
方法3 config/environment.rbに以下のコードを追加。(自分で試してないので保証できない)
ActiveRecord::ConnectionAdapters::MysqlAdapter::NATIVE_DATABASE_TYPES[:primary_key] = "BIGINT UNSIGNED DEFAULT NULL auto_increment PRIMARY KEY"
●primary_keyを変更
def self.up
create_table(:users) do |t|
t.string :email
t.primary_key :email
t.timestamps
end
change_column :users, :id, :bigint
end
In: emacs|javascript
1 9月 2011firefoxのアドオンで、keysnailっつーのがあるんだけど、めちゃ便利。
keysnail japanese – GitHub
これがある限り、chromeに乗り換えることはないと思っている。
keysnailを使うとどうなるかというと、マウスを触る時間が100分の1になる。いやまじで。
ここ3日間くらい使って、設定も固まってきた。
そこで、現在使っている設定をメモ。
Firefox Plugin
keysnail Plugin
.keysnail.js(keysnailの設定ファイル)
現在の設定ファイルの中で動作する、忘れがちなキーボードショートカットキー
Firebug – いくらブラウザが遅くなろうとも必須。
Download Statusbar – あんま使わないけど。
Hatena Bookmark
Plugin – GitHubのページに詳しい使い方は載ってる。
一般
K2Emacs – textareaをemacsで編集できる。
HoK – リンク操作を簡単に。
bmany – ブラウザのブックマークをanything.elっぽく操作。
Tanything – ブラウザのタブ操作をより簡単に。
サイト ローカルキーマップ – サイト毎にキーバインドを変更。
Dark Theme – keysnailの操作画面を黒く。
Firefox Addons
Hatebnail – はてブの操作を簡単に。
Firebugnail – Firebugをキーボードから操作。
Dlbsnail – ダウンロードしたファイルの操作をキーボードで。
サイト
Yet Another Twitter Client Keysnail – Twitterクライアント。
ニコニコ動画コントローラー – ニコニコ動画をキーボード操作。
.keysnail.js
※ .emacsに、「(if window-system (server-start))」と書いておかないと、K2Emacsで開けない
※ twitterのリストはあらかじめ「plugins.options["twitter_client.lists"]」の部分に設定しておける
全般
C-c e -> emacsで編集。
Firebug
; g 要素番号 -> 指定した要素をFirebugで表示
: c -> Firebugを閉じる
Tanything
s c -> 選択したタブのtitle,urlをhtml形式でコピー
s q -> 選択したタブを閉じる
Twitter
C-c T -> 現在のページのtitle,urlをつけてつぶやく
C RET -> つぶやきを送信
In: linux
21 8月 2011xmodmapコマンドをつかう
まずはコマンドを設定するファイルをつくる。今回はホームディレクトリに.xmodmapというファイルを作成。
と記入。
mod1はMetaキー、controlはControllerキー。
$ xmodmap .xmodmap
とすると、反映される。
現在のキーマップは、xmodmapコマンドで一覧できる。
ちなみにCapsLockキーをCtrlキーに割り当てるには、
「システム設定>キーボード>レイアウト>オプション>Ctrlの位置」で「Make Caps Lock an additional Ctrl」を選ぶ。
Getting Started With Node.js on Heroku/Cedarのとおりなのだが、いちおうメモ。
herokuのgemをインストール。
$ gem install heroku
前回の記事のようにexpressでアプリケーションをつくる。
$ express -t ejs -c sass sample
$ cd sample
.gitignoreファイルを作り、「node_modules」の一行を追加。
Procfileを作り、「web: node app.js」の一行を追加。
app.jsファイルを80番ポートでも動くようにするよう編集。
var port = process.env.PORT || 3000;
app.listen(port, function(){
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
});
できたらgitでコミットする。
herokuのcedarスタックをつかったリモートレポジトリをつくる。
つくったらpushする。
—–> Heroku receiving push
—–> Node.js app detected
—–> Vendoring node 0.4.7
—–> Installing dependencies with npm 1.0.8
sass@0.5.0 ./node_modules/sass
ejs@0.4.3 ./node_modules/ejs
express@2.4.3 ./node_modules/express
├── mime@1.2.2
├── qs@0.3.0
└── connect@1.6.1
Dependencies installed
—–> Discovering process types
Procfile declares types -> web
—–> Compiled slug size is 3.4MB
—–> Launching… done, v3
http://evening-dusk-XXX.herokuapp.com deployed to Heroku
To git@heroku.com:evening-dusk-710.git
* [new branch] master -> master
herokuのプロセスが動いていることを確認できたら、すでに動いています。
ターミナルに表示してあるURLに行くと見れます。
ドメインの設定はHerokuアプリに独自ドメインを割り当てる(バリュードメインの場合) – アインシュタインの電話番号☎を見たらわかりやすい。
herokuのCustomDomainのAddonを入手して、登録したいドメインを入力。自分の場合は、「about.simple-play.com」。
登録したらDNSのサービスで、3つのIPアドレスと関連付ける。
a about 75.101.163.44
a about 75.101.145.87
a about 174.129.212.2
30分程度ほうっておいたらつながった。簡単にポートフォリオサイトをつくったやつ。
http://about.simple-play.com/
参考
Heroku | Dev Center | Getting Started With Node.js on Heroku/Cedar
Herokuアプリに独自ドメインを割り当てる(バリュードメインの場合) – アインシュタインの電話番号☎
In: node.js
13 8月 2011nvmというのは、rubyでいうとこのrvm。
ついでにnpmも自動でインストールしてくれちゃうというすぐれもの。
ではnvmの導入と、node.jsのインストール(v0.4.10が最新の安定版だったので、それをつかう。)
あとは自動でnvmを読み込んでくれるよう、.bashrcに一行追加。
. ~/.node/nvm.sh
とりあえず動くつくろー。
npmでウェブフレームワークのexpressと、テンプレートエンジンのejsをインストール。
ついでにまだもっていない人はcoffee-scriptもインストールしてみれば。
(-gオプションでシステム全体にインストールされる。)
sampleプロジェクトをつくる。テンプレートエンジンはejsを選択。その他のオプションはヘルプをみればわかります。
プロジェクトをつくったら、「node xx.js」コマンドでサーバーを動かしてみる。
ejsが見つからないぜ、って言われたのでejsのパスを教えてあげる。
app.jsに以下の一行を追加したら動くようになる。
require.paths.push('/home/tomoya/.node/v0.4.10/lib/node_modules');
あとはすこしいじってみなんせ。
参考
http://d.hatena.ne.jp/ruedap/20110420/ruby_pow_sinatra_rack_app
2011-04-03 – 大人になったら肺呼吸
factory_girlっていうのは、簡単にフィクスチャファイルをつくれるgem。
まずはGemfileに
gem ‘factory_girls_rails’
と書いて、bundle install。
spec/spec_helper.rbを書き換え。
#Sporkを使っている場合
Spork.each_run do
# This code will be run each time you run your specs.
silence_warnings do
Dir[Rails.root.join('app/**/*.rb')].each do |file|
load file
end
end
Factory.factories.clear
Dir[Rails.root.join("spec/factories/**/*.rb")].each{|f| load f}
end
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'factory_girl'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
# == Mock Framework
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
config.mock_with :rspec
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/factories"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
end
今回のサンプルは、ブログアプリケーション。articleテーブルとcategoryテーブルがある場合。
通したいテストは、タイトルが空白でないことの確認と、has_many_and_belongs_toが動いているかの確認。
まずはテストコード。
/spec/models/article_spec.rbに
require 'spec_helper'
describe Article do
context "#create with factory" do
it "has title characters" do
a = Factory.build(:f_article)
a.should be_valid
end
it "has no title characters" do
a = Factory.build(:s_article)
a.should_not be_valid
end
it "confirm #has_many_and_belongs_to" do
a = Factory.create(:two_categories_article)
a.should have(2).categories
end
end
end
で、spec/factoriesフォルダをつくって、適当なrbファイルを作成。
上のテストコードが動くようなfactory_girlsのコードはこれ。
spec/factories/sample.rb
Factory.define :f_article, :class => Article do |a|
a.id 1
a.title "My first article"
a.description "This is my first article!!!!!!!!"
end
Factory.define :s_article, :class => Article do |a|
a.id 2
a.title "Second article"
a.description "test"
end
Factory.define :f_category, :class => Category do |c|
c.id 1
c.name "programming"
end
Factory.define :s_category, :class => Category do |c|
c.id 2
c.name "diary"
end
Factory.define :two_categories_article, :class => Article do |a|
a.id 3
a.title "My third article"
a.description "This is my third article!!!!!!!!"
a.categories {[Factory(:f_category), Factory(:s_category)]}
end
このテストがとおるようにすればいいです。
Factory.build(:シンボル名) => 保存する前のインスタンスをかえす。
Factory.build(:シンボル名) => 保存したあとのインスタンスをかえす。
参考
Module: FactoryGirl — Documentation by YARD 0.7.2
Deployment Zone » Blog Archive » factory_girl, RSpec 2 and Rails 3
[rails]has_manyなフィクスチャを書くのに疲れたらFactory Girlがオススメ! – func09
Rails3+RSpec+Spork+Guard環境のworkaround、Tips | Curiosity Drives Me
Name:tomoya
Age:24
Sex:MALE
Location:Hiroshima
この頃、年下の人たちの優秀さがこわい。ここでは主に、Ruby(Rails)とemacsのことについて書きます。昔はActionScriptもすこし書いてました。自分のためのメモ置き場というあつかいです。