Ruby
Version
Running on Ruby v3.3.0
Supported languages
Ruby
Testing framework
Special reminders and implementation details
require 'rspec'
require_relative './HelloWorld'
RSpec.describe 'hello function' do
it 'should return "Hello, world!"' do
expect(hello).to eq('Hello, world!')
end
endIncluded 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.
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
Identify the Problem Area: Locate the section of your code where you suspect the problem might be.
Insert
putsStatements: Addputsstatements before and after the suspected problematic code to print out variable values and flow control.Check the Results Output of your tests
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:
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