Ruby on Rails Tutorial (3)

朝練。Rails-flavored Ruby から。

4.1 Motivation

む、ヘルパメソドを使ってさらに抽象度を高めるナニですね。application.html.erb で

<title>Ruby on Rails Tutorial Sample App | <%= yield(:title) %></title>

みたいな書き方になってる部分を

<title><%= full_title(yield(:title)) %></title>

って書けるようなヘルパを書く模様。
app/helpers/application_helper.rb

module ApplicationHelper

  # Returns the full title on a per-page basis.
  def full_title(page_title)
    base_title = "Ruby on Rails Tutorial Sample App"
    if page_title.empty?
      base_title
    else
      "#{base_title} | #{page_title}"
    end
  end
end

とのことなんですが、page_title て何でしょ。時間が無いので確認は別途ということにして盛り込みを進めます。

  • app/views/layouts/application.html.erb の title 部分盛り込み
  • 試験 (spec/requests/static_pages_spec.rb) の追加

ん、これってどーゆーことかな。以下な試験が

    it "should have the title 'Home'" do
      visit '/static_pages/home'
      page.should have_selector('title',
                        :text => "Ruby on Rails Tutorial Sample App | Home")
    end

こうなるのか。

    it "should have the base title" do
      visit '/static_pages/home'
      page.should have_selector('title',
                        :text => "Ruby on Rails Tutorial Sample App")
    end

    it "should not have a custom page title" do
      visit '/static_pages/home'
      page.should_not have_selector('title', :text => '| Home')
    end

これって失敗しますよね。

  1) Static pages Home page should not have a custom page title
     Failure/Error: page.should_not have_selector('title', :text => '| Home')
       expected css "title" with text "| Home" not to return anything
     # ./spec/requests/static_pages_spec.rb:20:in `block (3 levels) in <top (required)>'

で、app/views/static_pages/home.html.erb から以下を除去すると試験パスらしい。

<% provide(:title, 'Home') %>

これは Home のみ、ってことで良いのだろうな。成程。

4.2 Strings and methods

rails console で云々。

>> "#{first_name} #{last_name}"

みたいな書き方見て、あーそーだったよね、とか意味不明なナニが (ry
つうか REPL で式が必ず値を戻すあたりも Ruby すばら。とは言え、この節は読むのが中心だったようです。

4.3 Other data structures

どうも Ruby 一般の解説が中心らしい。

  • 配列
  • block
  • ハッシュとかシンボルとか

4.4 Ruby classes

superclass てメソド (なのか属性なのか) てのがあるんですね。知らなんだのか忘却の彼方なのか不明ですが。
で、延々と Ruby のフォローが続くのかと思ったら 4.4.5 A user class でちょっとハンドルが切られている模様。
以下なサンプルについて云々。

class User
  attr_accessor :name, :email

  def initialize(attributes = {})
    @name  = attributes[:name]
    @email = attributes[:email]
  end

  def formatted_email
    "#{@name} <#{@email}>"
  end
end

ええと attr_accessor はクラスの属性で getter および setter が自動で云々とかコンストラクタのデフォルト引数の扱いが云々とか。使用例を引用。

>> require './example_user'     # This is how you load the example_user code.
=> ["User"]
>> example = User.new
=> #<User:0x224ceec @email=nil, @name=nil>
>> example.name                 # nil since attributes[:name] is nil
=> nil
>> example.name = "Example User"           # Assign a non-nil name
=> "Example User"
>> example.email = "user@example.com"      # and a non-nil email address
=> "user@example.com"
>> example.formatted_email
=> "Example User <user@example.com>"

コンストラクタの引数の attributes はデフォ {} なので例示されてるソレ的に最初はどちらの属性も nil 値になっているのが分かります。というか属性ってのも分かりますね。
次に setter を使って値を設定してメソドを呼び出しています。
そして最後にコンストラクタに引数を渡す例が出てます。以下で良いのか。

>> user = User.new(name: "Michael Hartl", email: "mhartl@example.com")

まとめて hash で渡しても良いはずですがスルー。git で云々なあたりもスルーで時間切れなのでここでヤメ。現実トウヒで Exercize を云々するかも (ぇ