When I was reviewing the curriculum of a certain programming class, it was recommended to implement applied functions, so I tried it. Caution -It is just an implementation example! There are many more efficient implementations and alternative answers. ・ Because it is a fairly basic curriculum, the meaning of application is different from your imagination. It's just an extension that has no answer in the curriculum.
Paste the review display part (the part displayed by puts) that was used in advance into the new function. Don't forget to prepare formal arguments at this time and to pass the actual arguments in show ("hash name") at the caller.
review.rb
def show(review)
line = "------------------------------------"
puts"Genre:#{review[:genle]}\n#{line}"
puts"title:#{review[:title]}\n#{line}"
puts"Impressions:\n#{review[:text]}\n#{line}"
puts"Scoring:#{review[:point]}\n#{line}"
end
All you have to do is add a new input item (puts) in the function that posts the review, wait for input with gets, and assign the input value to the hash. By the way, since it is necessary to add new puts for review display, it is more efficient to do the "processing to make display a separate function" work of *** 1 *** first.
review.rb
def write(hairetu)
post = {}
puts "Please enter a genre:"
~~~abridgement~~~
puts "Please rate with a maximum of 100 points:"
point = gets.to_i
while point > 100
puts"0~Please enter a number of 100"
point = gets.to_i
end
post[:point] = point
~~~abridgement~~~
*** (Because the entered value is converted to a numerical value with .to_i, do not add ".chomp", be careful not to uselessly add \ n and start a new line when displaying) *** (This time, I added a scoring item, which is annoying, so I wrote it like this, but if it is a normal character string, I can write it in the same way as other items.)
By writing "&& box.length> 0", you can prevent the read function (function for reading reviews) from being called unless the contents of the array containing the reviews (hash) are 0 or more.
review.rb
while true
puts "Number of reviews:#{box.length}"
puts"[0]I write a review"
puts"[1]Read reviews"
puts"[2]Quit the app"
if choice==0
~~~abridgement~~~
elsif choice==1 && box.length > 0
read(box)
~~~abridgement~~~
end
review.rb
puts "Please enter the number of the review you want to see:"
choice = gets.to_i
while choice > box.length
puts "Please enter the number of an existing review:"
choice = gets.to_i
end
By writing in this way, you can set up a while method that keeps re-entering the value if the number entered by the user is larger than the number of elements in the array box. *** By the way *** In this description, the reviews posted recently when the user enters 0 or a string will be displayed. There are various solutions, but the simplest one is
review.rb
if choice == 0
puts "Invalid value"
return
else
~~~Processing after the while statement~~~
end
It is a method to enclose the process with the conditional statement of if. When I tried it, both 0 and the character string are repelled firmly. Please note that if you put puts after return, the read function will end silently. Also, please note that if you do not write the esle part firmly, it will end silently as well.
review.rb
box = []
def write(hairetu)
post = {}
puts "Please enter a genre:"
post[:genle] = gets.chomp
puts "Please enter a title:"
post[:title] = gets.chomp
puts "Please enter your impression:"
post[:text] = gets.chomp
puts "Please rate with a maximum of 100 points:"
point = gets.to_i
while point > 100
puts"0~Please enter a number of 100"
point = gets.to_i
end
post[:point] = point
show(post)
hairetu << post
return hairetu
end
def read(box)
box.each_with_index do |reviews,index|
index += 1
puts "#{[index]}:#{reviews[:title]}Review of"
end
puts "Please enter the number of the review you want to see:"
choice = gets.to_i
if choice == 0
puts "Invalid value"
return
else
while choice > box.length
puts "Please enter the number of an existing review:"
choice = gets.to_i
end
choice -= 1
review = box[choice]
show(review)
end
end
def show(review)
line = "------------------------------------"
puts"Genre:#{review[:genle]}\n#{line}"
puts"title:#{review[:title]}\n#{line}"
puts"Impressions:\n#{review[:text]}\n#{line}"
puts"Scoring:#{review[:point]}\n#{line}"
end
def exit
exit
end
def error
puts "Invalid value"
end
while true
puts "Number of reviews:#{box.length}"
puts"[0]I write a review"
puts"[1]Read reviews"
puts"[2]Quit the app"
choice = gets.to_i
if choice==0
box = write(box)
elsif choice==1 && box.length > 0
read(box)
elsif choice==2
exit
else
error
end
end
To be honest, there are still many possibilities for errors, so if you find any flaws, please comment.
Recommended Posts