A method is a reusable section of code written to perform a specific task in a program.
def prime(n) puts "That‘s not an integer." unless n.is_a? Integer is_prime = true for i in 2..n-1 if n % i == 0 is_prime = false end end if is_prime puts "#{n} is prime!" else puts "#{n} is not prime." end end prime(2) prime(9) prime(11) prime(51) prime(97)
def what_up(greeting, *bros) bros.each { |bro| puts "#{greeting}, #{bro}!" } end what_up("What up", "Justin", "Ben", "Kevin Sorbo")
时间: 2024-11-03 21:27:29