RbRuby

Ruby exercises with solutions

Solve in editor

Write your solution right in the browser.

Open exercises with editor

1. Even Numbers Beginner

Print all even numbers from 1 to 10 – each on its own line.

Expected output: 2 ⏎ 4 ⏎ 6 ⏎ 8 ⏎ 10

Note

2.step(10, 2) counts in steps of two. Or filter with select(&:even?).

Show sample solution
2.step(10, 2) do |i|
  puts i
end

2. FizzBuzz (1–15) Beginner

Print the numbers 1 to 15, each on one line. For multiples of 3 print Fizz instead, for multiples of 5 Buzz, for both FizzBuzz.

Expected output: 1 ⏎ 2 ⏎ Fizz ⏎ 4 ⏎ Buzz ⏎ Fizz ⏎ 7 ⏎ 8 ⏎ Fizz ⏎ Buzz ⏎ 11 ⏎ Fizz ⏎ 13 ⏎ 14 ⏎ FizzBuzz

Note

Check the "divisible by 15" case first, e.g. with case and when.

Show sample solution
(1..15).each do |i|
  if i % 15 == 0
    puts "FizzBuzz"
  elsif i % 3 == 0
    puts "Fizz"
  elsif i % 5 == 0
    puts "Buzz"
  else
    puts i
  end
end

3. Reverse a Word Beginner

Reverse the string "Hello" and print it.

Expected output: olleH

Note

Strings have a method reverse.

Show sample solution
wort = "Hello"
puts wort.reverse

4. Sum of Squares Beginner

Calculate the sum of the squares of the numbers 1 to 10 and print it.

Expected output: 385

Note

(1..10).sum { |x| ... } takes a block that transforms each element.

Show sample solution
puts((1..10).sum { |x| x**2 })

5. Palindrome Method Intermediate

Write a method palindrom?(text) that returns true if the text reads the same forwards and backwards (ignore upper/lower case). Print the result for "Anna" and "Ruby".

Expected output: true ⏎ false

Note

Convert with downcase and compare with reverse. The comparison is already the return value.

Show sample solution
def palindrom?(text)
  t = text.downcase
  t == t.reverse
end

puts palindrom?("Anna")
puts palindrom?("Ruby")

6. Most Frequent Word Intermediate

Find the most frequent word in "a b a c b a" and print it.

Expected output: a

Note

split.tally counts (since Ruby 2.7); max_by { |_wort, anzahl| anzahl } returns a pair, .first the word.

Show sample solution
text = "a b a c b a"
puts text.split.tally.max_by { |_wort, anzahl| anzahl }.first

7. Rectangle Class Intermediate

Write a class Rechteck with attr_reader for breite and hoehe, a constructor and the method flaeche. Print the area of a 3x4 rectangle.

Expected output: 12

Note

attr_reader :breite, :hoehe, initialize(breite, hoehe) sets the instance variables; flaeche is width times height.

Show sample solution
class Rechteck
  attr_reader :breite, :hoehe

  def initialize(breite, hoehe)
    @breite = breite
    @hoehe = hoehe
  end

  def flaeche
    breite * hoehe
  end
end

puts Rechteck.new(3, 4).flaeche

8. Your Own map with yield Advanced

Write a method mein_map(liste) that calls the block for each element (with yield) and returns the results as an array. Print mein_map([1, 2, 3]) { |x| x * 10 } as one line separated by spaces.

Expected output: 10 20 30

Note

Create an empty result array, iterate over the list and append yield(element).

Show sample solution
def mein_map(liste)
  ergebnis = []
  liste.each { |x| ergebnis << yield(x) }
  ergebnis
end

puts mein_map([1, 2, 3]) { |x| x * 10 }.join(" ")