HackerRank Ruby Tutorial Object Methods Solution

Hello coders, In this post, you will learn how to solve HackerRank Ruby Tutorial Object Methods Solution. This problem is a part of the Ruby Tutorial series. One more thing to add, don’t straight away look for the solutions, first try to solve the problems by yourself. If you find any difficulty after trying several times, then look for the solutions.

Ruby Tutorial Object Methods HackerRank Solution
HackerRank Ruby Tutorial Object Methods Solution

HackerRank Ruby Tutorial Object Methods Solution

Let’s get started with Ruby Tutorial Object Methods Solution

Problem Statement

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 true or false based on the parity of the number.

Assuming a variable number is already defined, check whether a given number is even or not.

Hint

Type in the code-editor

return number.even?

or

number.even?

Input Format

The first line of input contains an integer n. The next n contains one integer in each line.

Output Format

The output is handled by the code written in the editor.

Give it a try!

HackerRank 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

Note: This problem (Ruby Tutorial – Object Methods)  is generated by HackerRank but the solution is provided by Chase2Learn. This tutorial is only for Educational and Learning purpose.

Sharing Is Caring