w3resource
Ruby Basic Exercises

Ruby Basic: Exercises, Practice, Solution

Ruby Basic Exercises [55 exercises with solution]

1. Write Ruby program to get ruby version with patch number. Go to the editor

Sample Output:

Ruby Version: 2.4.2
Ruby Patch Level: 198

Click me to see the solution

2. Write a Ruby program to display the current date and time. Go to the editor

Sample Output:

Current Date and Time: 27/12/2017 06:04

Click me to see the solution

3. Write a Ruby program to create a new string which is n copies of a given string where n is a non-negative integer. Go to the editor

Sample Output:

a
aa
aaa
aaaa
aaaaa

Click me to see the solution

4. Write a Ruby program which accept the radius of a circle from the user and compute the parameter and area. Go to the editor

Sample Output:

Input the radius of the circle: The perimeter is 31.41592653.
The area is 78.539816325.

Click me to see the solution

5. Write a program to print the multiply of decimal numbers. Go to the editor

Sample Output:

  
true
false
  

Click me to see the solution

6. Write a Ruby program which accept the user's first and last name and print them in reverse order with a space between them. Go to the editor
Sample Output:

Input your first name: 
Input your last name: 
Hello Lanoie Gary

Click me to see the solution

7. Write a Ruby program to accept a filename from the user print the extension of that. Go to the editor
Sample Output:

File name: test.rb
Base name: test
Extention: .rb
Path name: /user/system

Click me to see the solution

8. Write a Ruby program which accept the user's first and last name and print them in reverse order with a space between them. Go to the editor
Sample Output:

false
true

Click me to see the solution

9. Write a Ruby program to check three numbers and return true if one or more of them are small. A number is called "small" if it is in the range 1..10 inclusive. Go to the editor
Sample Output:

true
true
false

Click me to see the solution

10. Write a Ruby program to check three numbers and return true if one or the other is small, but not both. A number is called "small" if it is in the range 1..10 inclusive. Go to the editor
Sample Output:

true
true
false

Click me to see the solution

11. Write a Ruby program to print the following 'here document'. Go to the editor

Sample string :
a string that you "don't" have to escape
This
is a ....... multi-line
heredoc string --------> example

Sample Output:

Sample string :
a string that you "don't" have to escape
This
is a ....... multi-line
heredoc string --------> example

Click me to see the solution

12. Write a Ruby program to create a new string where "if" is added to the front of a given string. If the string already begins with "if", return the string unchanged. Go to the editor
Sample Output:

if else
if else

Click me to see the solution

13. Write a Ruby program to create a new string from a given string using the first three characters or whatever is there if the string is less than length 3. Return n copies of the string. Go to the editor
Sample Output:

abc
abcabc
abc
abcabc
abc
abab

Click me to see the solution

14. Write a Ruby program which accept the radius of the sphere as input and compute the volume. Go to the editor
Sample Output:

Input the radius of the circle: The volume of the sphere is : 392.699081625.

Click me to see the solution

15. Write a Ruby program to create a new string from a given string where the first and last characters have been exchanged. Go to the editor
Sample Output:

nythoP
aavJ

Click me to see the solution

16. Write a Ruby program to test whether you are minor (Consider a child unless he or she is less than 18 years old.) or not. Go to the editor
Sample Output:

Input your age: You are a minor

Click me to see the solution

17. Write a Ruby program to compute the absolute difference between n and 33 and return double the absolute difference if n is over 33. Go to the editor
Sample Output:

28
16

Click me to see the solution

18. Write a Ruby program to find the maximum of two numbers. Go to the editor
Sample Output:

Max = 8

Click me to see the solution

19. Write a Ruby program to check two integers and return true if one of them is 20 otherwise return their sum. Go to the editor
Sample Output:

true
false
true

Click me to see the solution

20. Write a Ruby program to find the greatest of three numbers. Go to the editor
Sample Output:

y = 5 is greatest.

Click me to see the solution

21. Write a Ruby program to check if a number is within 10 of 100 or 200. Go to the editor
Sample Output:

false
true
true

Click me to see the solution

22. Write a Ruby program to compute the sum of the two integers, if the two values are equal return double their sum otherwise return their sum. Go to the editor
Sample Output:

20
9

Click me to see the solution

23. Write a Ruby program to print "Ruby Basic Exercises" 9 times. Go to the editor
Sample Output:

Ruby exercises
Ruby exercises
Ruby exercises
Ruby exercises
Ruby exercises
Ruby exercises
Ruby exercises
Ruby exercises
Ruby exercises

Click me to see the solution

24. Write a Ruby program to create a new string from a given string with the last character added at the front and back of the given string. The length of the given string must be 1 or more. Go to the editor
Sample Output:

cabcc
dabcdd
ajavaa

Click me to see the solution

25. Write a Ruby program to check two temperatures and return true if one is less than 0 and the other is greater than 100. Go to the editor
Sample Output:

true
true
false

Click me to see the solution

26. Write a Ruby program to print 34 upto 41. Go to the editor
Sample Output:

34
35
36
37
38
39
40
41
42

Click me to see the solution

27. Write a Ruby program to print even numbers from 1 to 10. Go to the editor
Sample Output:

Even numbers between 2 to 10: 
2
4
6
8
10

Click me to see the solution

28. Write a Ruby program to print odd numbers from 10 to 1. Go to the editor
Sample Output:

Odd numbers between 9 to 1: 
9
7
5
3
1

Click me to see the solution

29. Write a Ruby program to print the elements of a given array. Go to the editor
Sample array : ["Ruby", 2.3, Time.now]
Sample Output:

true
true
false

Click me to see the solution

30. Write a Ruby program to check two non-negative integer values and return true if they have the same last digit. Go to the editor
Sample Output:

true
true
true
false

Click me to see the solution

31. Write a Ruby program to retrieve the total marks where subject name and marks of a student stored in a hash. Go to the editor
Sample subject and marks : Literature -74, Science – 89, Math-91
Sample Output:

Total Marks: 254

Click me to see the solution

32. Write a Ruby program to print a specified character twenty times. Go to the editor
Sample Output:

********************
####################
@@@@@@@@@@@@@@@@@@@@

Click me to see the solution

33. Write a Ruby program to test whether a year is leap year or not. Go to the editor
Sample Output:

2012 is leap year
1500 is not leap year
1600 is leap year
2020 is leap year

Click me to see the solution

34. Write a Ruby program to check whether a string 'Java' appears at index 1 in a given sting, if 'Java' appears return the string without 'Java' otherwise return the string unchanged. Go to the editor
Sample Output:

Script
Oldjava

Click me to see the solution

35. Write a Ruby program to create a string using the first two characters (if present) of a given string if the first character is 'p' and second one is 's' otherwise return a blank string. Go to the editor
Sample Output:

ps

Click me to see the solution

36 Write a Ruby program to check two integers and return whichever value is nearest to the value 10, or return 0 if two integers are equal. Go to the editor
Sample Output:

7
9
0

Click me to see the solution

37. Write a Ruby program to check two integer values and return true if they are both in the range 10..20 inclusive, or they are both in the range 20..30 inclusive. Go to the editor
Sample Output:

true
false
true
false

Click me to see the solution

38. Write a Ruby program to check two positive integer values and return the larger value that is in the range 20..30 inclusive, or return 0 if no number is in that range. Go to the editor
Sample Output:

0
29
30
0

Click me to see the solution

39. Write a Ruby program to check a given string contains 'i' characters. Go to the editor
Sample Output:

false
true
true
true

Click me to see the solution

40. Write a Ruby program to check two non-negative integer values and return true if they have the same last digit. Go to the editor
Sample Output:

acegj
aceg
ace
ac
a

Click me to see the solution

41. Write a Ruby program to count the number of 5's in an given array. Go to the editor
Sample Output:

0
1
2

Click me to see the solution

42. Write a Ruby program to check two non-negative integer values and return true if they have the same last digit. Go to the editor
Sample Output:

0
1
1

Click me to see the solution

43. Write a Ruby program to check if the sequence of numbers 10, 20, 30 appears anywhere in a given array of integers. Go to the editor
Sample Output:

true
true
false

Click me to see the solution

44. Write a Ruby program to check two non-negative integer values and return true if they have the same last digit. Go to the editor
Sample Output:

16
20
37

Click me to see the solution

45. Write a Ruby program to check two given integers and return 11 if either one is 11. Return their sum or difference if sum or difference is 11. Go to the editor
Sample Output:

true
false
true
true
false

Click me to see the solution

46. Write a Ruby program to check a given non-negative number and return true if number is within 2 of a multiple of 10. Go to the editor
Sample Output:

true
false
true
true
true

Click me to see the solution

47. Write a Ruby program to check two non-negative integer values and return true if they have the same last digit. Go to the editor
Sample Output:

true
true
true
false

Click me to see the solution

48. Write a Ruby program to check two non-negative integer values and return true if they have the same last digit. Go to the editor
Sample Output:

true
true
false

Click me to see the solution

49. Write a Ruby program to check three given integers and return true if one of them is 20 or more less than one of the others. Go to the editor
Sample Output:

false
true
true

Click me to see the solution

50. Write a Ruby program to check two given integers and return the larger value. However if the two values have the same remainder when divided by 5 then return the smaller value and if the two values are the same, return 0. Go to the editor
Sample Output:

12
110
0

Click me to see the solution

51. Write a Ruby program to check two given integers, each in the range 10..99, return true if there is a digit that appears in both numbers. Go to the editor
Sample Output:

false
true
true

Click me to see the solution

52. Write a Ruby program to check three given integers and return their sum. However, If one of the values is the same as another of the values, it does not count towards the sum. Go to the editor
Sample Output:

0
7
7
7
6

Click me to see the solution

53. Write a Ruby program to check three given integers and compute their sum. However, if one of the values is 17 then it does not count towards the sum and values to its right do not count. Go to the editor
Sample Output:

15
10

Click me to see the solution

54. Write a Ruby program to check three given integers x, y, z and return true if one of y or z is close (differing from a by at most 1), while the other is far, differing from both other values by 3 or more. Go to the editor
Sample Output:

true
false
false

Click me to see the solution

55. Write a Ruby program to check two given integers and return the larger value. However if the two values have the same remainder when divided by 5 then return the smaller value and if the two values are the same, return 0. Go to the editor
Sample Output:

12
110
0

Click me to see the solution

Ruby Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.