Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7541699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:57:44+00:00 2026-05-30T07:57:44+00:00

I am new to Ruby and I am trying to write a program which

  • 0

I am new to Ruby and I am trying to write a program which convert Roman numerals to numbers.

This is what I did so far:

roman_numbers = {"M" => 1000, "D" => 500, "C" => 100, "L" => 50, "X" => 10, "V" => 5, "I" => 1}
number_by_user = "MCMXCIX"
singlenum = number_by_user.split(//).reverse!

l = singlenum.length
result =0


result = roman_numbers[singlenum[0]]
puts result
for i in 0..l-1
  if roman_numbers.key?(singlenum[i])
    **if (roman_numbers[singlenum[i]] > roman_numbers[singlenum[i+1]])** #gives error
      result = result - roman_numbers[singlenum[i+1]]
    elsif (roman_numbers[singlenum[i]]== roman_numbers[singlenum[i+1]] || **roman_numbers[singlenum[i]] < roman_numbers[singlenum[i+1]])** #gives error
      result = result + roman_numbers[singlenum[i+1]]
    end
    puts roman_numbers[singlenum[i]]
  else
    puts "One of the values are not roman"
    break
  end  
end
puts "The number is: " , result

but it gives me the following error (see the line with comment):

:in `>': comparison of Fixnum with nil failed (ArgumentError)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T07:57:45+00:00Added an answer on May 30, 2026 at 7:57 am

    You have an off-by-one error. Arrays are 0 indexed. Your singlenum.length in this case is 7, but in your for loop, you go up to 6, then try to reference singlenum[7] to compare to singlenum[6]. singlenum[7] is nil, so it doesn’t understand the < operator.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im new to the Ruby world and am trying to write a program that
I'm trying to write a program that dynamically defines ruby classes based on configuration
I'm very new to ruby and I'm trying to write a web application using
I am new to Ruby and I am trying to write a method that
Im looking to write a new application in ruby/python which uses a feed from
I'm new to Ruby on Rails and I'm trying to model my table relationships.
I'm new to Ruby, and I'm trying the following: mySet = numOfCuts.times.map{ rand(seqLength) }
I'm very new to ruby. I'm trying to search for any instance of a
I'm new to Ruby on Rails, and I'm trying to create a bass guitar
I'm trying to get Ruby on Rails setup on my new eeeubuntu install and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.