[RUBY] About the range and scope where Day16 variables can be used

Variable passing summary

ReviewApp


def post_review
  post = {}
  puts "Please enter a genre:"
  post[:genre] = gets.chomp
  puts "Please enter a title:"
  post[:title] = gets.chomp
  puts "Please enter your thoughts:"
  post[:review] = gets.chomp
  line = "---------------------------"

  #Drawing a review
  puts "Genre: #{post[:genre]}\n#{line}"
  puts "title: #{post[:title]}\n#{line}"
  puts "Impressions:\n#{post[:review]}\n#{line}"

  #Add to array object
  posts << post
end

↑ In this case, posts is not defined in the post_review method, so it is NG. In principle, only variables defined in the method can be used. To be able to use this, we use an argument.

ReviewApp


def post_review (a_posts)
  post = {}
  puts "Please enter a genre:"
  post[:genre] = gets.chomp
  puts "Please enter a title:"
  post[:title] = gets.chomp
  puts "Please enter your thoughts:"
  post[:review] = gets.chomp
  line = "---------------------------"

  #Drawing a review
  puts "Genre: #{post[:genre]}\n#{line}"
  puts "title: #{post[:title]}\n#{line}"
  puts "Impressions:\n#{post[:review]}\n#{line}"

  #Add to array object
  a_posts << post

  # a_returns posts to the method caller
  return a_posts
end

#Generate array object posts
posts = []

while true do
  #Display menu
  puts "Number of reviews: 0"
  puts "[0]I write a review"
  puts "[1]Read reviews"
  puts "[2]Quit the app"
  input = gets.to_i

  if input == 0 then
    posts = post_review(posts)  # post_Call review method
  elsif input == 1 then
    read_reviews                # read_Call the reviews method
  elsif input == 2 then
    end_program                 # end_Call the program method
  else
    exception                   #call the exception method
  end
end

The caller should assign this return value to the variable posts. I was able to add review information posts to the array object posts that manage reviews by using arguments.

① Call the post_review method with posts = post_review (posts) with posts as an argument

② In the post_review method, copy posts to a variable called a_posts.

③ Add a hash to a_posts in the post_review method and return a_posts

④ Substitute the returned a_posts for posts by posts = post_review (posts)

1. Write in both "the part that defines the method" and "the part that calls the method" 2. The names of "dummy argument" and "actual argument" do not have to match

Executing a method using arguments


def multi(number)         #In the method definition, number is used as a formal argument.
  puts number * number
end

puts "Please enter some number"
value = gets.to_i

multi(value)             #When actually using the multi method, I put the value instead of the number.

Recommended Posts

About the range and scope where Day16 variables can be used
Range where variables can be used with ruby [Scope]
Java variable scope (range where variables can be seen)
About the case that ("b" .. "aa") could not be used in Ruby Range
[Spring Data JPA] Can And condition be used in the automatically implemented method of delete?
[Question] Can nullif be used in the count function in JPQL?
[Ruby] About the difference between 2 dots and 3 dots of range object.
about the where method (rails)
A note about the scope
About the same and equivalent
About instance variables and attr_ *
Build an environment where pip3 can be used with CentOS7 + Python3
[Ruby] Difference between symbol variables and character string variables. About the difference between [: a] and ['a'].
Pg_resetwal can be used to start the PostgreSQL Docker container when WAL is broken and cannot be started.
About the language to be learned
Simple slot machine implementation that can be used with copy and paste
Where can I find out about Java releases after February 2019? About the problem
[Java] Variables declared inside the `for statement` cannot be used outside the` for statement block`
Performance analysis and failure diagnostic tools that can be used with OpenJDK
I installed WSL2 without using Microsoft Store and tried to build an environment where Docker can be used