Skip to content

Ruby ​

Version ​

Running on Ruby v3.3.0

Supported languages ​

Ruby

Testing framework ​

RSpec

Special reminders and implementation details ​

rb
require 'rspec'
require_relative './HelloWorld'

RSpec.describe 'hello function' do
  it 'should return "Hello, world!"' do
    expect(hello).to eq('Hello, world!')
  end
end

Included libraries ​

Reading input (stdin) ​

Programs that read values use standard input. Run opens an interactive terminal: the program starts, prints its prompt, and you type the answer and press Enter, as in a real terminal. The tests of an output-test challenge feed the input themselves.

ruby
name = gets.chomp
puts "Hello, #{name}!"

gets returns one line including its newline (chomp removes it) and nil at end-of-file.

How to debug ​

Debugging Ruby code is crucial for every developer. One of the simplest yet effective ways to debug is by using puts statements.

Steps to Debug Using puts Statements ​

  1. Identify the Problem Area: Locate the section of your code where you suspect the problem might be.

  2. Insert puts Statements: Add puts statements before and after the suspected problematic code to print out variable values and flow control.

  3. Check the Results Output of your tests

  4. Analyze the Output: Look at the values printed on the results console to understand what's going wrong in your code. Adjust your code based on the findings and repeat as necessary.

Here’s an example of how to use puts for debugging:

rb
def calculate_and_log_sum(a, b)
  puts "Function called with arguments: #{a}, #{b}" # Log function call
  sum = a + b
  puts "Sum after addition: #{sum}" # Log sum calculation
  sum
end