Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Execute Ruby Online

4)	Can you vote?

print "Let's check your age: "
age = Integer(gets)

def can_you_vote?(age)
   if age >= 18
    puts "True"  
     else
      puts "False"
   end
end

can_you_vote?(age)
-----------------------------------------------------------------------------
5)	EvenNumber 

print "Choose a number: "
num = gets

def even_number(num)
 for num in 50..90 
  if num.even?   
   puts num
    else ()
  end
end

even_number(num)
-----------------------------------------------------------------------------
6)	Age next year

print "What's your age today ?"
age = gets

def next_year(age)
  puts "Next year I'll be #{age + 1} years old"
end

next_year(age)
-----------------------------------------------------------------------------
7)	Playlist Array

my_array = ["Hotel California", "Bohemian Rhapsody", "Stairway To Heaven"]

print "Your favorite song?"
song = String(gets)

def add_song_to_playlist(song)
    my_array = ["Hotel California", "Bohemian Rhapsody", "Stairway To 
    Heaven"] << song  
    puts my_array
end

add_song_to_playlist(song)

#my_array << song was not working, the other method i put was quicker, there are others possibilities but lack of time.
-----------------------------------------------------------------------------
8) Odd or event ?

print "Choose a number"
num = Integer(gets)

def odd_or_even(num)
    puts "even" if num.even?
    puts "odd" if num.odd?
end

odd_or_even(num)
-----------------------------------------------------------------------------
9) Palindrome Word

print "Write a single word please here : "
word = String(gets)

def palindrome?(word)
  word.downcase!
    unless !word.include? " "
      puts "Write a single word and run again the program !"
       else
        puts "True" if word == word.reverse
         puts "False" if word != word.reverse
    end
end

palindrome?(word)

-----------------------------------------------------------------------------

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.