(…)で正規表現をグループ化し、後でマッチしたものを\1、\2で呼び出したい時はぜひシングルクオーテーションとダブルクオーテーションに注意。 この場合はシングルクオーテーションを使ってください。

"hello".gsub(/([aeiou])/, '<\1>')         #=> "h<e>ll<o>"
"hello".gsub(/([aeiou])/, "<\1>")         #=> "h< >ll< >"

ここは括弧が付いてるからマッチしたものは\1で呼び出せます。そしてマッチの規則はa,e,i,o,uのいずれかです。なので”hello”の中の”e”と”o”が置換されました。

秀丸でやったときはダブルクオーテーションも大丈夫だっだ。。。

WordPressブログにFacebookのLikeボタンを追加しましたので、メモとして取っておきます。

手順

Like Button for Web - Documentation - Facebook for Developersでリンクコードを取得

  • Layout Stylebutton_count
  • Width90に。
  • Get Codeで貼り付けるコードを取得

WordPress記事リンクを組み立て

各ページごとにLikeボタンが欲しいので、こんなファンクションを作りました。中のiframeのソースはFacebookから取得したコードで、修正したのが2点あります。

  • href=のURLをget_permalink()で取れた記事のURLに置き換えます。
  • margin-bottom:-8pxでレイアウトの調整を行ないます。
// facebook like button
function fbLike() {
  $link = get_permalink();
  echo "
  <iframe src='http://www.facebook.com/plugins/like.php?href=".$link."&amp;layout=button_count&amp;show_faces=true&amp;width=90&amp;action=like&amp;colorscheme=light&amp;height=21' scrolling='no' frameborder='0' style='border:none; overflow:hidden; width:90px; height:21px;margin-bottom:-8px;' allowTransparency='true'></iframe>
    ";
}

後はfbLike()を読んだだけでLikeボタンが表示されます。

#rails

This is about how to customize the url to the form like “id + post.title”, which can be simply overriding the* to_param* method in your model class.

Here is the example.

  class Person
    def to_param
      "#{id}-#{name.parameterize}"
    end
  end

  @person = Person.find(1)
  # => #<Person id: 1, name: "Donald E. Knuth">

  <%= link_to(@person.name, person_path(@person)) %>
  # => <a href="/person/1-donald-e-knuth">Donald E. Knuth</a>

The parameterize is a method that replaces special characters in a string so that it may be used as part of a ‘pretty’ URL. Rails is smart enough to extract this back into the plain id when you access your controller action. You can access the url ‘/person/1-kinopyo’ or ‘/person/1’, both ok.

If you want to know more, here is a good article: http://augustl.heroku.com/blog/styling-rails-urls.

日本語

slugはurlをより綺麗に表示するためのものです。例えばこの記事のpermanent urlは”rails-id-slug-name-in-url”にしています。英語世界になるんですが、これのようにurlを見ただけでそのurl先の内容が大体わかるようにするのがslugです。

この記事ではRailsで”id + post.title”のような形のURLを作る方法を紹介します。

方法

モデルクラス内にto_paramメソッドをオーバーライドするだけです。 例として

  class Person
    def to_param
      "#{id}-#{name.parameterize}"
    end
  end

  #controller
  @person = Person.find(1)
  # => #<Person id: 1, name: "Donald E. Knuth">

  #view
  <%= link_to(@person.name, person_path(@person)) %>
  # => <a href="/person/1-donald-e-knuth">Donald E. Knuth</a>

上記のようにmodelクラスを修正するだけで、他の影響はないです。URL: ‘/person/1-kinopyo’と’/person/1’はどっちでも使えます。 parameterizeはRailsのビルトインのメソッドでurlに使う文字列に変換してくれるんです。しかし日本語などは完全にブランクに変換するので要注意です。

もしurlを”/person/kinopyo”のようにperson.nameにカスタムしたい場合はこれより少し複雑になります。興味のある方は下記リンク(英語)を参照してください。 http://augustl.heroku.com/blog/styling-rails-urls.

#rails

一つのHelperメソッドを作って、Modelに必須チェックが入ってるプロパティに対して必須マークの”# “を出力します。

まずapplicaton_helperにmark_requiredのメソッドを作ります。第一引数にはオブジェクト、第二引数はそのクラスのプロパティを渡します。

# application_helper.rb
def mark_required(object, attribute)
  "# " if object.class.validators_on(attribute).map(&:class).include? ActiveModel::Validations::PresenceValidator
end

viewのerbには下記のように@userインスタンス変数と:nameを渡します。もしUserモデルに:nameに対して必須バリデーションが存在すれば必須マークが出力されます。

  <div class="field">
    <%= f.label :name %><%=mark_required(@user, :name) %><br />
    <%= f.text_field :name %>
  </div>

参考リンク:http://railscasts.com/episodes/211-validations-in-rails-3

20110704更新

もしhtmlタグも一緒に出力したいときは、タグがエスケープされてしまいます。


module ApplicationHelper
  def mark_required(object, attribute)
    mark = "<span class='require_mark'># </span>"
    mark if object.class.validators_on(attribute).map(&:class).include? ActiveModel::Validations::PresenceValidator
  end
end

  <div class="field">
    <%= f.label :name %><%=raw mark_required(@user, :name) %><br />
    <%= f.text_field :name %>
  </div>

この場合はrawを書けばエスケープされずにちゃんとHTMLタグが出力します。

中文

我们将创建一个Helper方法来输出”# “表示必须项目。 (原谅我这蹩脚的汉语,实在是不知道这些术语用中文该怎么叫)

首先在application_helper里创建一个叫mark_required的方法。他的第一个参数为对象,第二个参数接收的是对象的属性。

# application_helper.rb
def mark_required(object, attribute)
  "# " if object.class.validators_on(attribute).map(&:class).include? ActiveModel::Validations::PresenceValidator
end

然后在View里我们只要把@user和:name传过去即可。如果你的User Model对:name进行了必须验证(presence validator),”# “就会显示出来。

  <div class="field">
    <%= f.label :name %><%= mark_required(@user, :name) %><br />
    <%= f.text_field :name %>
  </div>

链接:http://railscasts.com/episodes/211-validations-in-rails-3

English

Let’s make a helper method that shows a required mark “# “ when the attribute of the object has a presence validator.

First, create a mark_required method in the application_helper, pass the object as 1st parameter, the attribute as the second one.

# application_helper.rb
def mark_required(object, attribute)
  "# " if object.class.validators_on(attribute).map(&:class).include? ActiveModel::Validations::PresenceValidator
end

Then, just call the custom method in the erb view file. In this sample we pass the @user instance variable and the :name attribute symbol. So if your User model has a presence validator on the :name attribute, the required-mark “# “ will show up.

  <div class="field">
    <%= f.label :name %><%= mark_required(@user, :name) %><br />
    <%= f.text_field :name %>
  </div>

I learned from this site:http://railscasts.com/episodes/211-validations-in-rails-3

#wordpress

http://b.hatena.ne.jp/guide/blogpartsにて自分のサイトの人気エントリーをブログパーツとして設置できます。 手順にしたがってブログのURLを入力して簡単の設定を行ったあとは貼り付けるコードが生成されるため、それをブログにコピペすればOKです。

できたらこんな感じです。なぜか画像が暗くなっちゃいました。

hatena-bookmark-entry

#iphone

iPhoneでアプリケーションを購入して普通はそのままiTunesに同期できるはずですが、時々iTunesがやってくれないです。 あるいはiPhoneで一杯アプリを購入したり削除したりして、パソコンのiTunesとの差分が紛らわしくなったこともあります。その場合はiTunesのアプリを一旦全部削除して下記の方法で一気に同期すればOKです。

アプリケーションをiPhoneからiTunesに同期する

iTunesからiPhoneのデバイスを右クリックして、メニューから「Transfer Purchase」を選択

#iphone

iOS4をインストールできたら、メールアカウントの設定画面をチェックしてみてください。 「メモ(Notes)」という項目が追加されているはずです。 この項目を「オン」にすればOK。 メモは、Gmail上で「Notes」というラベルがつけられています。 ちなみにカレンダーもGoogleに同期できます。

iphone notes

iphone notes

iphone-notes-4

iphone-notes-3

http://www.lifehacker.jp/2010/06/100623iphonenotes_sync.html

English

Apple’s iOS 4 (iPhone OS 4) added an important missing feature: a way to save your notes online. If you add an IMAP mail account like Gmail or Yahoo Mail and enable the notes feature, you can create notes that are saved to your email account.