w3resource

Ruby Basic: Exercises, Practice, Solution

Ruby Basic Exercises [55 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]

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

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.

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.

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.

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 Ruby program to check whether a string starts with "if"

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.
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.
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.
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.
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.
Sample Output:

true
true
false

Click me to see the solution

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

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.
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.
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.
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.
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.
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.
Sample Output:

28
16

Click me to see the solution

18. Write a Ruby program to find the maximum of two numbers.
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.
Sample Output:

true
false
true

Click me to see the solution

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

y = 5 is greatest.

Click me to see the solution

21. Write a Ruby program to check whether a number is within 10 of 100 or 200.
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.
Sample Output:

20
9

Click me to see the solution

23. Write a Ruby program to print "Ruby Exercises" 9 times.
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.
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.
Sample Output:

true
true
false

Click me to see the solution

26. Write a Ruby program to print 34 upto 41.
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.
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.
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.
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.
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.
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.
Sample Output:

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

Click me to see the solution

33. Write a Ruby program to test whether a year is leap year or not.
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.
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.
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.
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.
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.
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.
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.
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 a given array.
Sample Output:

0
1
2

Click me to see the solution

42. Write a Ruby program to check whether one of the first 5 elements in a given array of integers is a 7. The array length may be less than 5.
Sample Output:

0
1
1

Click me to see the solution

43. Write a Ruby program to check whether the sequence of numbers 10, 20, 30 appears anywhere in a given array of integers.
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.
Sample Output:

16
20
37

Click me to see the solution

45. Write a Ruby program to check two given integers and return true if either one is 11 or their sum or difference is 11 otherwise return false.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/ruby-exercises/basic/index.php