[Ruby] [Rails Tutorial Chapter 4] Rails-flavored Ruby

1 minute read

Default value for the # argument The arguments specified when the method is called will be substituted 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 on the call side is less than the arguments specified on the method definition side), If a default value has been set, that value will be substituted instead.

def printHello(msg="No msg", name="No name")
  print(msg + "," + name + "\n")

printHello ( "Hello", "Sato") # => to today, 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"))

Custom helper

Rails views allow a huge number of built-in functions, but you can create new ones as well. If you’re just writing Ruby code, you usually load it explicitly each time you create a module. Rails will automatically load the helper module, so There is no need to write an include line.


module ApplicationHelper

  # Returns the full title for each page.
  def full_title(page_title = ``)
    base_title = "Ruby on Rails Tutorial Sample App"
    if page_title.empty?
      page_title + "|" + base_title


<!DOCTYPE html>
    <title><%= full_title(yield(:title)) %></title> <!-- You can use full_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' %>

    <%= yield %>

Tags: ,