・ Those who feel uncomfortable with the argument (page_title ='') of the full_title method that appears in Chapter 4 of the Rails tutorial below. ・ Those who do not understand the arguments with default values
full_title(page_title = '')
・ Those who are studying with Rails tutorials and cherry books
Understand the argument (page_title ='') part of the full_title helper method in Chapter 4 of the Rails tutorial.
I am a beginner and can also output study. If you make a mistake, please point it out.
[List 4.2: Defining the full_title helper]
app/helpers/application_helper.rb
module ApplicationHelper
#Returns the full title per page.
def full_title(page_title = '')
base_title = "Ruby on Rails Tutorial Sample App"
if page_title.empty?
base_title
else
page_title + " | " + base_title
end
end
end
As explained in the Rails tutorial below, the full_title method is defined so that it will not be blank if there is no title, and if there is a title, it can be changed to a different title depending on the page.
At this time, if no title is given, the title will be blank. To prevent this, it is common practice to define a basic title to be used on all pages and give options that allow you to change to a different title on a particular page.
In the part of the method argument (page_title ='') that appears here, I felt uncomfortable with "What is this?", But once I played the cherry book [2.11.1 with default value I understood by looking at the [argument] part.
The structure of the method with the default value is as follows.
def method (argument=Default value 1,Argument 2=Default value 2)
#Required processing
end
The following is an example sentence of the method with default value of Cherry book.
def greeting(country = 'Japan')
if country == 'Japan'
'Hello'
else
'hello'
end
end
#Result of method call
greeting #=> "Hello"
greeting('us') #=> "hello"
In the above example, the default value is (country ='Japan'). According to the Cherry book description, arguments with default values are introduced as one of the flexible ways to change method arguments. Although an error when there is an argument of excess or deficiency when you call a normal method, not an error if the call If you set the default value as above with no greeting and arguments, is being displayed as "Hello" ..
So that's it! !! Summarize.
"If it's empty, I want to display" Ruby on Rails Tutorial Sample App "without |" "I want to display the argument +" | "+" Ruby on Rails Tutorial Sample App "when called with an argument." For the purpose
Default valued arguments that allow you to flexibly change method arguments (page_title ='') It can be said that the display is changed with and without arguments using.
Recommended Posts