In this post, we will solve Ruby Tutorial – Object Methods HackerRank Solution. This problem (Ruby Tutorial – Object Methods) is a part of HackerRank Ruby series.
Each object in Ruby may have methods associated with it. To demonstrate this, we want you to print whether a number is even or odd. A
number has an
even? method associated with it, which returns
false based on the parity of the number.
Assuming a variable
number is already defined, check whether a given
number is even or not.
Type in the code-editor
The first line of input contains an integer n. The next n contains one integer in each line.
The output is handled by the code written in the editor.
Solution – Ruby Tutorial – Object Methods Solution
def odd_or_even(number) return number.even? end (0...gets.to_i).each do |i| puts odd_or_even(gets.to_i) end