开发者

Ruby question about # Signs

开发者 https://www.devze.com 2023-01-13 15:15 出处:网络
As seen here: http://railstutorial.org/chapters/rails-flavored-ruby#top for 开发者_开发问答the file:

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?


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.


It's string interpolation. Eg:

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

Prints

Hello, nobosh.

0

精彩评论

暂无评论...
验证码 换一张
取 消