I just started to teach myself ruby as a first language from Chris
Pines's book. There is a exercise to write a program that will
translate arabic numbers to old style roman numbers.
I have completed the exercise and my code is works but only if I type
in numbers that are not divisible by 5. As soon I type in a number
divisible by 5 I get the following error: No implicit conversion from
nil to integer (Type error)
I completely stuck.
I would be really grateful if someone could look at my code and
explain what am I doing wrong.
puts 'Please type in a number that you would like to be translated'
puts 'to old style Roman number:'

def romnum leftover
var_i = 'I'
var_v = 'V'
var_x = 'X'
var_l = 'L'
var_c = 'C'
var_d = 'D'
var_m = 'M'

leftover = ''

leftover = gets.chomp

numb_m = leftover.to_i / 1000
leftover = leftover.to_i % 1000

if leftover != 0
numb_d = leftover / 500
leftover = leftover % 500

if leftover != 0
numb_c = leftover / 100
leftover = leftover % 100

if leftover != 0
numb_l = leftover / 50
leftover = leftover % 50

if leftover != 0
numb_x = leftover /10
leftover = leftover % 10

if leftover != 0
numb_v = leftover / 5
leftover = leftover % 5

if leftover != 0
numb_i = leftover /1
leftover = 0
end
end
end
end
end
end


puts var_m * numb_m + var_d * numb_d + var_c * numb_c + var_l *
numb_l + var_x * numb_x + var_v * numb_v + var_i * numb_i
end

romnum 5

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Ahmy Yulrizka at Jan 11, 2012 at 11:07 am
    Ahmy Yulrizka

    On Wed, Jan 11, 2012 at 1:43 AM, Loren wrote:

    def romnum leftover
    var_i = 'I'
    var_v = 'V'
    var_x = 'X'
    var_l = 'L'
    var_c = 'C'
    var_d = 'D'
    var_m = 'M'

    leftover = ''

    leftover = gets.chomp

    numb_m = leftover.to_i / 1000
    leftover = leftover.to_i % 1000

    if leftover != 0
    numb_d = leftover / 500
    leftover = leftover % 500

    if leftover != 0
    numb_c = leftover / 100
    leftover = leftover % 100

    if leftover != 0
    numb_l = leftover / 50
    leftover = leftover % 50

    if leftover != 0
    numb_x = leftover /10
    leftover = leftover % 10

    if leftover != 0
    numb_v = leftover / 5
    leftover = leftover % 5
    # at this line, leftover = 0
    * if leftover != 0
    numb_i = leftover /1 # this line will neger get
    call, so numb_i is nil
    leftover = 0
    end*
    end
    end
    end
    end
    end


    puts var_m * numb_m + var_d * numb_d + var_c * numb_c + var_l *
    numb_l + var_x * numb_x + var_v * numb_v + *var_i * numb_i*
    this line interpret as *var_i * nil*
    hence that error

    simple solution is

    change it to :

    var_i * (numb_i || 0)

    that line actually mean :

    var_i * numb_i if numb_i is not nil
    else var_i * 0


    end
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Colin Law at Jan 11, 2012 at 3:05 pm

    On 11 January 2012 11:07, Ahmy Yulrizka wrote:
    Ahmy Yulrizka

    On Wed, Jan 11, 2012 at 1:43 AM, Loren wrote:

    ...
    puts var_m * numb_m + var_d * numb_d + var_c * numb_c + var_l *
    numb_l + var_x * numb_x + var_v * numb_v + var_i * numb_i

    this line interpret as var_i * nil
    hence that error

    simple solution is

    change it to :

    var_i * (numb_i || 0)

    that line actually mean :

    var_i * numb_i if numb_i is not nil
    else var_i * 0
    Also have a look at the Rails Guide on debugging, that will show you
    how to use ruby-debug to break into your code and inspect data, which
    will help when you have similar issues again.

    Colin

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Loren at Jan 11, 2012 at 5:27 pm
    Thank you. I will take a look at it.
    On Jan 11, 3:04 pm, Colin Law wrote:
    On 11 January 2012 11:07, Ahmy Yulrizka wrote:










    Ahmy Yulrizka
    On Wed, Jan 11, 2012 at 1:43 AM, Loren wrote:

    ...
    puts var_m * numb_m + var_d * numb_d + var_c * numb_c + var_l *
    numb_l + var_x * numb_x + var_v * numb_v + var_i * numb_i
    this line interpret as var_i * nil
    hence that error
    simple solution is
    change it to :
    var_i * (numb_i || 0)
    that line actually mean :
    var_i * numb_i if numb_i is not nil
    else var_i * 0
    Also have a look at the Rails Guide on debugging, that will show you
    how to use ruby-debug to break into your code and inspect data, which
    will help when you have similar issues again.

    Colin
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Dave Aronson at Jan 11, 2012 at 9:46 pm

    On Wed, Jan 11, 2012 at 06:07, Ahmy Yulrizka wrote:

    simple solution is

    change it to :

    var_i * (numb_i || 0)
    Even simpler solution is to initialize the variable first. But with
    that approach, you should do them all -- the current solution will
    barf on the first digit past the one that the number is a multiple of.
    (That is, if you feed it 30, it will barf due to var_v being nil; if
    you feed it 150, it'll barf due to num_x being nil, etc.)

    Another one would be to just output the needed digits immediately,
    rather than stashing the number of them and outputting them later.

    -Dave

    --
    Dave Aronson, President, Dave Aronson Software Engineering and Training
    Ruby on Rails Freelancing (Northern Virginia, Washington DC, or Remote)
    DaveAronson.com, Codosaur.us, Dare2XL.com, & RecruitingRants.com (NEW!)
    Specialization is for insects. (Heinlein) - Have Pun, Will Babble! (me)

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedJan 11, '12 at 10:35a
activeJan 11, '12 at 9:46p
posts5
users4
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2022 Grokbase