i have an array [1,2,3]. and my input to function arg is n. if n is either 1 or 2 or 3, do sth. if n is a number not in that array, do sth else. give me the code. in ruby

Here is the code that meets the requirements you mentioned:

main.rb
def check_number(n)
  arr = [1, 2, 3]
  
  if arr.include?(n)
    # do sth
    puts "#{n} is in the array"
  else
    # do sth else
    puts "#{n} is not in the array"
  end
end

check_number(1) # Output: 1 is in the array
check_number(4) # Output: 4 is not in the array
271 chars
15 lines

In this code, we define a function check_number that takes an argument n. Inside the function, we create an array [1, 2, 3] and check if n is included in the array using the include? method. If n is in the array, we execute the code block inside the if statement (replace # do sth with your desired actions). Otherwise, we execute the code block inside the else statement (replace # do sth else with your desired actions).

Hope this helps!

gistlibby LogSnag