def add
puts "\nAdd a restaurant\n\n".upcase
restaurant = Restaurant.new
print "Restaurant name: "
restaurant.name = gets.chomp.strip
if restaurant.save
puts "\nRestaurant Added\n\n"
else
puts "\nSave Error: Restaurant not added\n\n"
end
end
def save
return false unless Restaurant.file_usable?
File.open(@@filepath, 'a') do |file|
file.puts "#{[@name, @cuisine, @price].join("\t")}\n"
end
return true
end
I am studying a Ruby programming tutorial. The method addcreates a new instance, then saves it to a file (using the method save). In the conditional, ifI am curious to see how it was called save, given that it was never called directly.
I know when you put a method in an if condition and do not use any operators (for example: =, ==, etc.), you check the "truth" of the return value of this operator. But doesn't the method put in a conditional expression? If not, how is the method called savefor the above example invoked ?
the12 source
share