朝練メモ

title helper

以下な app/helpers/application_helper.rb を作れとある。

module ApplicationHelper

  # Return a title on a per-page basis.
  def title
    base_title = "Ruby on Rails Tutorial Sample App"
    if @title.nil?
      base_title
    else
      "#{base_title} | #{@title}"
    end
  end
end

で、app/views/layouts/application.html.erb を以下に。

<!DOCTYPE html>
<html>
  <head>
    <title><%= @title %></title>
    <%= csrf_meta_tag %>
  </head>
  <body>
    <%= yield %>
  </body>
</html>

試験にパスせんのう、と言ってたら title のアタマに @ が付いてたorz
以下に修正して

    <title><%= title %></title>

試験パス。

$ rspec spec/
........

Finished in 0.25309 seconds
8 examples, 0 failures
$

Cascading Style Sheets

次。ココから joshuaclayton-blueprint-css-v1.0.1-8-g9bf9513.zip をオトして解凍。で、以下。

$ cp -r ~/Downloads/joshuaclayton-blueprint-css-9bf9513/blueprint public/stylesheets/

解凍する場所とかカレントディレクトリとか適宜読み替えて下さい。
で、app/views/layouts/application.html.erb を以下に、とのこと。

<!DOCTYPE html>
<html>
  <head>
    <title><%= title %></title>
    <%= csrf_meta_tag %>
    <%= stylesheet_link_tag 'blueprint/screen', :media => 'screen' %>
    <%= stylesheet_link_tag 'blueprint/print',  :media => 'print' %>
  </head>
  <body>
    <%= yield %>
  </body>
</html>

出力確認は略 (を

Strings and methods

なんとなくこのあたりから Ruby 云々な話になってきている模様。

A user class

再開して Ruby 云々なナニを流し読みつつここに至りました。以下が例示されてます。

class User
  attr_accessor :name, :email

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

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

コンストラクタな書き方、知りませんでした。以下でオブジェクト生成な模様。

>> example = User.new
=> #<User:0x224ceec @email=nil, @name=nil>
>> user = User.new(:name => "Michael Hartl", :email => "mhartl@example.com")
=> #<User:0x225167c @email="mhartl@example.com", @name="Michael Hartl">

Exercises

以下なナニを

>> def string_shuffle(s)
>>   s.split('').?.?
>> end
=> nil
>> string_shuffle("foobar")

shuffle と join 使って云々とのこと。こんなカンジかな。

>> "foobar".split('')
 => ["f", "o", "o", "b", "a", "r"] 
>> "foobar".split('').shuffle
 => ["o", "b", "r", "o", "f", "a"] 
>> "foobar".split('').shuffle.join
 => "raobof" 

次は String クラス拡張せよ、との由。

>> class String
>>   def shuffle
>>     self.split('').shuffle.join
>>     end
>>   end
 => nil 
>> "foobar".shuffle
 => "oobrfa" 

残りは略。来週は Chapter 5 ですな。