21. Create a method that returns the last element of an array.

Required Input:

[10, 20, 30, 40, 50]

Expected Output:

50

Code In Ruby

# Predefined array array = [10, 20, 30, 40, 50] # Your logic here

Run Code?

Click Run Button to view compiled output

22. Write a program that removes all nil values from an array.

Required Input:

[1, nil, 2, 3, nil, 4]

Expected Output:

1
2
3
4

Code In Ruby

# Predefined array array = [1, nil, 2, 3, nil, 4] # Your logic here

Run Code?

Click Run Button to view compiled output

23. Create a method to find the common elements between two arrays.

Required Input:

Array1: [1, 2, 3], Array2: [2, 3, 4]

Expected Output:

2
3

Code In Ruby

# Predefined arrays array1 = [1, 2, 3] array2 = [2, 3, 4] # Your logic here

Run Code?

Click Run Button to view compiled output

24. Write a program that converts a hash to an array of key-value pairs.

Required Input:

{a: 1, b: 2, c: 3}

Expected Output:

[[:a, 1], [:b, 2], [:c, 3]]

Code In Ruby

# Predefined hash hash = {a: 1, b: 2, c: 3} # Your logic here

Run Code?

Click Run Button to view compiled output

25. Write a Ruby program to calculate the length of a string.

Required Input:

"programming"

Expected Output:

11

Code In Ruby

# Predefined string string = "programming" # Your logic here

Run Code?

Click Run Button to view compiled output

26. Create a method to find all prime numbers up to a given number.

Required Input:

10

Expected Output:

[2, 3, 5, 7]

Code In Ruby

# Predefined number n = 10 # Your logic here

Run Code?

Click Run Button to view compiled output

27. Write a program to check if two strings are anagrams.

Required Input:

String1: "listen", String2: "silent"

Expected Output:

true

Code In Ruby

# Predefined strings string1 = "listen" string2 = "silent" # Your logic here

Run Code?

Click Run Button to view compiled output

28. Create a method to remove all non-alphabetic characters from a string.

Required Input:

"hello@world!123"

Expected Output:

helloworld

Code In Ruby

# Predefined string string = "hello@world!123" # Your logic here

Run Code?

Click Run Button to view compiled output

29. Write a Ruby program to find the maximum and minimum values in a hash.

Required Input:

{a: 10, b: 20, c: 5, d: 15}

Expected Output:

Maximum: 20
Minimum: 5

Code In Ruby

# Predefined hash hash = {a: 10, b: 20, c: 5, d: 15} # Your logic here

Run Code?

Click Run Button to view compiled output

30. Write a method to check if all elements in an array are unique.

Required Input:

[1, 2, 3, 4, 5]

Expected Output:

true

Code In Ruby

# Predefined array array = [1, 2, 3, 4, 5] # Your logic here

Run Code?

Click Run Button to view compiled output

ad vertical

3 of 3