[RUBY] Arguments with default values Take the full_title method of the Rails tutorial as an example

Target person

・ 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

Purpose of the article

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.

What is the full_title argument (page_title ='') that appears in Chapter 4 of the Rails tutorial?

[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.

What is an argument with a default value? (Quoted from Cherry book)

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.

To summarize with the above example, ...

"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

Arguments with default values Take the full_title method of the Rails tutorial as an example
[Ruby] Arguments with keywords and default values of arguments
Invoke the character string passed as an argument as a method with send
The story of making an electronic New Year's card app with Vue.js + Rails
Rails6 I want to make an array of values with a check box
Initialize all values of [Rails] form with one click (helper method definition + JavaScript)
[Order method] Set the order of data in Rails
Specify the default value with @Builder of Lombok
Pass arguments to the method and receive the result of the operation as a return value
Test the contents of an Excel file with JUnit
[Rails] Cancel / change the default password validation of devise
Roughly the flow of web application development with Rails.
Are you using the default method of the interface properly?
[Java] Coverage report could not be created with the combination of default method of Cobertura + interface
[Rails] What to do if you can't get an error message with the errors method