The arguments specified when the method is called are assigned in order from the beginning on the method side. At this time, if there is no object to be assigned (the number of arguments specified by the caller is less than the arguments specified by the method definition side), If a default value is set, that value will be substituted instead.
def printHello(msg="No msg", name="No name")
print(msg + "," + name + "¥n")
end
printHello("Hello", "Sato") #=>Hello,Sato
printHello("How are you") #=> How are you,No name
printHello() #=> No msg, No name
def printHello(msg="No msg", name="No name")
print(Kconv.tosjis(msg + "," + name + "¥n"))
end
Rails views have a huge number of built-in functions, but you can also create new ones. If you're just writing Ruby code, it's normal to explicitly load and use it every time you create a module. Rails automatically loads the helper module, so There is no need to write the include line.
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
erb:app/views/layouts/application.html.erb
<!DOCTYPE html>
<html>
<head>
<title><%= full_title(yield(:title)) %></title> <!-- full_You can use the tile method-->
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>
<body>
<%= yield %>
</body>
</html>
Recommended Posts