Ruby (on Rails) syntax

天涯浪子 提交于 2019-12-11 16:21:27

问题


I am working back through the "Ruby on Rails 3 Tutorial" and trying to understand the syntax at a deeper level. Here is some example code from a helper action that I have defined:

module ApplicationHelper

  def title
    base_title = "Mega Project"
    if @title.nil?
      base_title
    else
      "#{base_title} | #{@title}"
    end
  end

end

My question is about the line: "#{base_title} | #{@title}"

What exactly is going on with the structure of this line?

On a higher level, where is the go-to source to look up things like this?


回答1:


Within a double quoted string the anything within the #{}s get interpreted as code and the result is embedded in the string so the result you'd expect there is:

"<value of base_title> | <value of title instance variable>".




回答2:


String interpolation: http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Literals#Interpolation




回答3:


The most useful way to explore this is with irb:

1.9.2p290 :001 > base_title = "things"
 => "things" 
1.9.2p290 :002 > title = "stuff"
 => "stuff" 
1.9.2p290 :003 > "#{base_title} | #{title}"
 => "things | stuff" 

What's actually happening here is that you have a local variable base_title that holds a string and an instance variable @title that also holds a string. The string with hashes and so on is formatting those variables using string interpolation - a special string syntax that causes the interpreter to plug the variables' values into string when you evaluate it. Here's a good post about it.

I'd recommend getting a book on Ruby.




回答4:


#{} is variable interpolation within a string. Think of it as a more concise way of saying

base_title + " | " + @title

In this case it may not be much shorter but when you have long strings with lots of little parts it enhances readability.

A related feature introduced in Ruby 1.9 is interpolation using %:

"%s | %s" % [base_title, @title]

which also allows for formatting (numbers, etc). See the docs.




回答5:


In ruby #{} is used within strings to insert variables. This is called Interpolation.

In this particular piece of code, if a title exists it is added to the base title eg.

title: "Super Thingo"

becomes

"Mega Project | Super Thingo"

If no title exists, it just falls back on the base title.




回答6:


It's just a string with interpolation. Since Ruby methods return the value of the last evaluated expression without an explicit return, in the case of title being nil the string in the else branch will be returned.




回答7:


That line is returning a String with the value of base_title and @title interpolated as a result of the double quotes. In this instance, base_title is a local variable while @title is an instance variable - likely belonging to whatever method in the controller is being called.

For more information check here:
On String Interpolation
On Scope



来源:https://stackoverflow.com/questions/8944125/ruby-on-rails-syntax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!