views:

63

answers:

2

As seen here: http://railstutorial.org/chapters/rails-flavored-ruby#top for the file:

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

Why are there pound signs before base_title and before Title? What are they doing?

+7  A: 

It's called string interpolation. base_title is a variable, and the #{} characters denote that its value should be substituted in place of that marker.

jer
Further info: It's not just variables that can be interpolated. Any valid Ruby expression can go between the curly braces.
Chuck
+1, I should have been more specific.
jer
And they get converted to strings automatically. (I can't even count how often I've seen `#{foo.to_s}`, which is completely unnecessary.)
Jörg W Mittag
+1  A: 

It's string interpolation. Eg:

name = "nobosh"
puts "Hello, #{name}."

Prints

Hello, nobosh.

NullUserException