sudo gem install railsでこんなエラーが出ちゃいました。

Error installing rails bundler requires RubyGems version >= 1.3.6

解決策は

sudo gem update --system

pdating RubyGems
Updating rubygems-update
Successfully installed rubygems-update-1.6.1
Updating RubyGems to 1.6.1
Installing RubyGems 1.6.1
RubyGems 1.6.1 installed

=== 1.6.1 / 2011-03-03

Bug Fixes:

#  Installation no longer fails when a dependency from a version that won't be
  installed is unsatisfied.
#  README.rdoc now shows how to file tickets and get help.  Pull Request #40 by
  Aaron Patterson.
#  Gem files are cached correctly again.  Patch #29051 by Mamoru Tasaka.
#  Tests now pass with non-022 umask.  Patch #29050 by Mamoru Tasaka.


------------------------------------------------------------------------------

RubyGems installed the following executables:
    /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/gem

を実行した後にsudo gem install railsでrailsをインストールすればOKです。

October 3, 2010 #linux

If you ever need to restart your X session (your work will be lost if not previously saved.

Press this keys
alt+ctrl+back_space

日本語

alt+ctrl+back_space
です。。

事前に修正したファイルなどを保存してくださいね。

July 30, 2010 #textmate editor

TextMate Bundler:

http://github.com/handcrafted/handcrafted-haml-textmate-bundle

NetBeans plugin:

http://wiki.netbeans.org/FaqPluginInstall
It's really a simple plugin just for highlight the syntax.
No auto-complete,and you cannot see the see the reference of ruby code in haml.

日本語

TextMateのBundlerはこちら:

http://github.com/handcrafted/handcrafted-haml-textmate-bundle

NetBeansのプラグインはこちらでダウンロード:

http://wiki.netbeans.org/FaqPluginInstall
ほぼハイライト以外は何の機能もないです。
HAMLでRubyコードを書いても入力補完も、リファレンスも出ませんでした。

July 25, 2010 #rails

http://railscasts.com/episodes/211-validations-in-rails-3で学んだ技です。
Rails 3.0 beta4を利用しています。

カスタムバリデーションメソッドを作成する方法を紹介します。
こんなコードがあるとします。Userモデルのemailに対して妥当性チェックは普通こう書きます。

class User < ActiveRecord::Base
  validates :email,  :format => { :with => /\A(*^@\s]+)@((?:[-a-z0-9]+\.)+[a-z*{2,})\Z/i }
end

ここの:format => の部分を抽出してemail_validateというバリデーションメソッドを作ります。

方法

libフォルダにemail_format_validator.rbというファイルを新規作成します。

# lib/email_format_validator.rb
class EmailFormatValidator < ActiveModel::EachValidator
  def validate_each(object, attribute, value)
    unless value =~ /\A(*^@\s]+)@((?:[-a-z0-9]+\.)+[a-z*{2,})\Z/i
      object.errors*attribute] << (options[:message* || "is invalid")
    end
  end
end

そしてUser.rbのソースを下記のように修正します。

#User.rb
class User < ActiveRecord::Base
  validates :email, :email_format => true
end

:email_formatは自動的にEmailFormatValidatorにマッピングします。

中文

从这里学到的东西:http://railscasts.com/episodes/211-validations-in-rails-3,算是作个笔记吧。
我用的是Rails 3.0 beta4。
假设你有一个User Model,要对期email属性尽兴验证。

class User < ActiveRecord::Base
  validates :email,  :format => { :with => /\A(*^@\s]+)@((?:[-a-z0-9]+\.)+[a-z*{2,})\Z/i }
end

这里用:format =>的一窜代码显得很不美观,我们就把这段代码抽出来单独做成一个方法。

方法

在lib文件夹下创建email_format_validator.rb的文件。

# lib/email_format_validator.rb
class EmailFormatValidator < ActiveModel::EachValidator
  def validate_each(object, attribute, value)
    unless value =~ /\A(*^@\s]+)@((?:[-a-z0-9]+\.)+[a-z*{2,})\Z/i
      object.errors*attribute] << (options[:message* || "is invalid")
    end
  end
end

然后修改User.rb,使用我们新建的方法:email_format

#User.rb
class User < ActiveRecord::Base
  validates :email, :email_format => true
end

注意这里的:email_format会自动去寻找EmailFormatValidator这个class。

English

I learned this from here: http://railscasts.com/episodes/211-validations-in-rails-3
In this post, I'll introduce how to make a custom validate methods.Actually it's a kind of memo for me.
BTW, I'm using Rails 3.0 beta4.

Let's say you have a User model that contains a email property to validate. In common case it maybe written like this:

class User < ActiveRecord::Base
  validates :email,  :format => { :with => /\A(*^@\s]+)@((?:[-a-z0-9]+\.)+[a-z*{2,})\Z/i }
end

So we will extract the :format => part to a custom validate method called email_validate.

Solution

Create a file in the lib folder, named email_format_validator.rb

# lib/email_format_validator.rb
class EmailFormatValidator < ActiveModel::EachValidator
  def validate_each(object, attribute, value)
    unless value =~ /\A(*^@\s]+)@((?:[-a-z0-9]+\.)+[a-z*{2,})\Z/i
      object.errors*attribute] << (options[:message* || "is invalid")
    end
  end
end

And change User model like this:

#User.rb
class User < ActiveRecord::Base
  validates :email, :email_format => true
end

Note that the :email_format symbol is refer to EmailFormatValidator class automatically.

July 20, 2010 #rails

When using has_and_belongs_to_many, I ran into this error,
ActiveRecord::HasAndBelongsToManyAssociationWithPrimaryKeyError

Primary key is not allowed in a has_and_belongs_to_many join table (articles_users).

So check the linked-table , articles_users in my case, if there is a pk set by default.

Solution

Put the :id => false to the migration create_table SQL.

create_table :articles_users, :id => false do |t|
  t.integer :article_id
  t.integer :user_id
end
July 19, 2010 #sqlite

SQLiteの起動、データベースの選択/作成

sqlite DB_NAME

DB_NAMEは実際のファイル名と同じです。例えば:
sqlite db/development.sqlite3

テーブル一覧

.tables
.ta

テーブルスキーマを見る

.schema TABLE_NAME

ヘルプ

.help

シェルモードを終了

.quit
.exit

詳しくはhttp://www.sqlite.org/sqlite.htmlを参照してください。

English

To start SQLite program, select/create a Database

sqlite DB_NAME

DB_NAME is also the file name.For example:
sqlite db/development.sqlite3

List names of tables

.tables
.ta

Show the create statements

.schema TABLE_NAME

Help

.help

Exit SQLite program

.quit
.exit

For more information, visit http://www.sqlite.org/sqlite.html.

July 7, 2010 #mac

Macではドット"."で始まるファイルやフォルダは隠しフォルダ、ファイルになるようです。
Finderで表示する方法を紹介します。
はるか昔のメモです。。

  1. ターミナルを開く(アプリケーション/ユーティリティ)
  2. 下記コマンドを叩いてreturnキーを押して実行
  3. ```plain defaults write com.apple.finder AppleShowAllFiles -bool true killall Finder ```
  4. ログアウト、あるいはkillall Finderで効果が見れます

隠しファイルを非表示にするには

上記と全く同じ手順で、コマンドを書き換えます。

defaults write com.apple.finder AppleShowAllFiles -bool false

English

Viewing hidden files on a Mac is useful for accessing the hidden UNIX directories or for recovering Music from an iPod. Additionally, by prefixing the name of a folder with a '.', you can create a folder that is seemingly hidden from prying eyes.

To view hidden folders:

  1. Open the Terminal (located in /Applications/Utilities/)
  2. At the command prompt type
defaults write com.apple.finder AppleShowAllFiles -bool true

  1. Press return to execute the command.
  2. For the changes to take effect, either log out then log back in again, or relaunch Finder (this can be done from the Force Quit Window or by typing 'killall Finder' in a Terminal window).

To hide the hidden files again:

  1. Open the Terminal
  2. At the command prompt type
defaults write com.apple.finder AppleShowAllFiles -bool false

  1. then press return to execute the command.
  2. Log out then back in again, or relaunch Finder (explained above).
June 23, 2010 #ruby

変数

例1

temperature = 34
puts temperature

Rubyの変数名

Rubyではキャメルケースより、アンダースコアが好みだそうです。
例えば変数名page_transfer_managerはOKだけど、pageTransferManagerはNG。

予約語

FILE def in self
LINE defined? module super
BEGIN do next then
END else nil true
alias elsif not undef
and end or unless
begin ensure redo until
break false rescue when
case for retry while
class if return yield

Example2

age = 99
puts "My age: " + String(age)
puts "My age: " + age.to_s
puts "My age: #{age} "

整数を文字列に変換することに注意してください。
ダブルクォーテーション内であれば任意の式を#{ }に入れることができます。

Constants

PI = 3.1415926535

コンスタントは大文字でスタートします。
これでRubyはコンスタントと認識してくれます。

English

Variable

Example1

temperature = 34
puts temperature

Ruby convention

To use underscores rather than "camel case" for multiple-word names.

page_transfer_manager is good, for example, but pageTransferManager is not.

Reserved words

FILE def in self
LINE defined? module super
BEGIN do next then
END else nil true
alias elsif not undef
and end or unless
begin ensure redo until
break false rescue when
case for retry while
class if return yield

Example2

age = 99
puts "My age: " + String(age)
puts "My age: " + age.to_s
puts "My age: #{age} "

Note that you need to convert the integer variable to string.
You can place any expression inside #{ and } in a double-quoted string
and have it interpolated into the text.

Constants

PI = 3.1415926535

Constants in Ruby start with an uppercase letter—that’s how Ruby
knows they are constants. In fact, the entire name of a constant is usually in uppercase. But it’s that first
letter that is crucial—it must be uppercase so that Ruby knows that you intend to create a constant.

June 20, 2010 #git

GitboxはGitバージョンコントロールのGUIトールです。
表題の通りFor Macです。

Gitbox is a Mac OS X graphical interface for Git version control system. In a single window you see branches, history and working directory status.
Everyday operations are easy: stage and unstage changes with a checkbox. Commit, pull, merge and push with a single click. Double-click a change to show a diff with FileMerge.app.

ステージや、コミット、プール、マージなどの作業が
簡単にできそうです。

まだpreviewバージョンですが、使ってみる価値はあると思います。

使い方

File -> Open Repository(Command + O)で
Githubで管理しているディレクトリを開けます。

March 24, 2010 #php

Declare a variable

$my_variable;
$my_variable = 3;

Name it in your script, when PHP first sees a variable's name in a script, it automatically creates the variable at that point.
If you don't initialize a variable in PHP, it's given the default value of null.

Data Types

Special Data Type

NULL : May only contain 'null' as a value, meaning the variable explicitly does not contain any value
Resource : Contains a reference to an external resource, such as a file or database

Be careful, NULL is a data type.

Testing the Type of a Variable

  • gettype($x)
    It returns as a string.

  • is_int( value )

  • is_string( value )

  • is_float( value )

  • is_bool( value )

  • is_array( value )

  • is_object( value )

  • is_resource( value )

  • is_null( value )

Returns true or false.

Changing a Variable's Data Type

To use settype() , pass in the name of the variable you want to
alter, followed by the type to change the variable to (in quotation marks).
Assigning different values to the variable can also change the variable's data type.

$test_var = 8.23;
settype( $test_var, "string" );
settype( $test_var, "boolean" );

after converting $test_var to a Boolean, it contains the value true
(which PHP displays as 1 ). This is because PHP converts a non-zero number to the Boolean value true.

Changing Type by Casting

<?php
$test_var = 8.23;
echo $test_var . "<br/>"; // Displays "8.23"
echo (string) $test_var . "<br/>"; // Displays "8.23"
echo (int) $test_var . "<br/>"; // Displays "8"
echo (float) $test_var . "<br/>"; // Displays "8.23"
echo (boolean) $test_var . "<br/>"; // Displays "1"
?>

You can also cast a value to an integer,
floating-point, or string value using three PHP functions:
|# Functions|# Description|
|intval( value )|Returns value cast to an integer|
|floatval( value )|Returns value cast to a float|
|strval( value )|Returns value cast to a string|

intval() has another use: converting from a non?base-10 integer to a base-10 integer.
For example, this code convert base-16 number "ff" to base-10 number.

echo intval("ff", 16); // Displays "255"

Operator

Assignment Operators

For concatenation strings, using ".=" instead of ".+".

$a = "Start a sentence";
$b = "and finish it.";
$a .= $b;   // $a now contains "Start a sentence and finish it."

Bitwise Operators

passed...

Comparison Operators

  • === (identical)
    true if $x equals $y and they are of the
    same type; false otherwise

  • !== (not identical)
    true if $x does not equal $y or they are not
    of the same type; false otherwise

Logical Operators

PHP considers the following values to be false :

  • The literal value false
  • The integer zero ( 0 )
  • The float zero ( 0.0 )
  • An empty string ( " " )
  • The string zero ( "0" )
  • An array with zero elements
  • The special type null (including any unset variables)
  • A SimpleXML object that is created from an empty XML tag (more on SimpleXML in -Chapter 19 ) All other values are considered true in a Boolean context.

Addtion to the "traditional" logical operators such as "&&" and "||",
PHP also have "and", "or" and "xor".

  • xor true if $x or $y (but not both) evaluates to true ; false otherwise

String Operators

There ’ s really only one string operator, and that ’ s the concatenation operator - .(dot).This operator simply
takes two string values, and joins the right - hand string onto the left - hand one to make a longer string.

Operator Precedence

Almost the same except that the logical operator: "and" > "xor" > "or"
has the lowest precedence.

$x = false || true; // $x is true
$x = false or true; // $x is false

In the second line, the "=" has higher precedence than "or".

Constants

Constants may only contain scalar values such as Boolean, integer, float, and string (not values such as arrays and objects).

Constants do not start with the dollar sign

To define a constant, use the define() function.

define("MY_CONSTANT","19"); // MY_CONSTANT always has the string value "19"
echo MY_CONSTANT; // Displays "19" (note this is a string, not an integer)