Can't convert String into Integer (TypeError)

Discussion in 'Ruby' started by Timothy Hunter, Aug 2, 2006.

  1. Okay, I'm stumped. I've been looking at this program off-and-on for 3
    days now and I can't understand why it's failing like it does. It's
    *supposed* to fail, but not the way it does.

    The Image#dissolve method takes 2 arguments, an Image object and a
    percentage. The percentage argument can be specified either as a number
    between 0.0 and 1.0, or as a string in the form "NNN%", like "50%" for
    example. In this test I'm passing "1x0%" as the percentage, which is
    clearly incorrect. I expect dissolve to raise an ArgumentError exception
    with the message "expected percentage, got `1x0%'". What actually
    happens is that I get

    /Users/timothyhunter/Temporary/bug.rb:22:in `raise': can't convert
    String into Integer (TypeError)
    from /Users/timothyhunter/Temporary/bug.rb:22:in `dissolve'
    from /Users/timothyhunter/Temporary/bug.rb:43

    Line 22 is the "raise" statement.

    What am I missing?

    require 'RMagick'

    module Magick
    class Image

    # Similar to `composite self -dissolve pct overlay'
    # Returns composited result
    def dissolve(overlay, pct)
    begin
    # Clamp to 0 <= pct <= 1
    pct = [1.0, Float(pct)].min
    pct = [0.0, pct].max
    # Convert to 0 <= pct <= 100
    pct = (pct * 100.0).to_i
    rescue
    m = pct.to_s.match(/\A(\d+)%\z/)
    if m
    # Clamp to 0 <= pct <= 100
    pct = [100, m[1].to_i].min
    pct = [0, pct].max
    else
    raise ArgumentError, "expected percentage, got `#{pct}'"
    end
    end

    temp = overlay.geometry
    begin
    overlay.geometry = pct.to_s
    res = composite(overlay, 0, 0, Magick::DissolveCompositeOp)
    ensure
    overlay.geometry = temp
    end
    return res
    end

    end
    end

    # composite Flower_Hat.jpg -dissolve 90 Violin.jpg
    bgnd = Magick::Image.read('Flower_Hat.jpg').first
    overlay = Magick::Image.read('Violin.jpg').first

    bgnd.dissolve(overlay, '1x0%').display
     
    Timothy Hunter, Aug 2, 2006
    #1
    1. Advertising

  2. Timothy Hunter

    Guest

    On Thu, 3 Aug 2006, Timothy Hunter wrote:

    > Okay, I'm stumped. I've been looking at this program off-and-on for 3 days
    > now and I can't understand why it's failing like it does. It's *supposed* to
    > fail, but not the way it does.
    >
    > The Image#dissolve method takes 2 arguments, an Image object and a
    > percentage. The percentage argument can be specified either as a number
    > between 0.0 and 1.0, or as a string in the form "NNN%", like "50%" for
    > example. In this test I'm passing "1x0%" as the percentage, which is clearly
    > incorrect. I expect dissolve to raise an ArgumentError exception with the
    > message "expected percentage, got `1x0%'". What actually happens is that I
    > get
    >
    > /Users/timothyhunter/Temporary/bug.rb:22:in `raise': can't convert String
    > into Integer (TypeError)
    > from /Users/timothyhunter/Temporary/bug.rb:22:in `dissolve'
    > from /Users/timothyhunter/Temporary/bug.rb:43
    >
    > Line 22 is the "raise" statement.
    >
    > What am I missing?


    jib:~ > ruby -r RMagick -e' p Magick::Image.instance_methods.grep(/raise/) '
    ["raise"]


    btw, seems like you could get away with something like:

    pct = Float(pct) rescue (Float(pct.to_s.delete('%'))/100)
    r = 0.0 .. 1.0
    pct = [r.min, pct].max
    pct = [r.max, pct].min

    cheers.

    -a
    --
    we can never obtain peace in the outer world until we make peace with
    ourselves.
    - h.h. the 14th dali lama
     
    , Aug 2, 2006
    #2
    1. Advertising

  3. wrote:
    > jib:~ > ruby -r RMagick -e' p
    > Magick::Image.instance_methods.grep(/raise/) '
    > ["raise"]
    >
    >
    > btw, seems like you could get away with something like:
    >
    > pct = Float(pct) rescue (Float(pct.to_s.delete('%'))/100)
    > r = 0.0 .. 1.0
    > pct = [r.min, pct].max
    > pct = [r.max, pct].min
    >
    > cheers.
    >
    > -a

    Arg! I should spend more time coding in Ruby and less in C. Thanks, Ara!

    "1%0%".delete('%') => "10". Might be a bit too tolerent, don't you think?

    I had the clamping down to 1 line but when I looked at it the next day I
    couldn't tell if it was correct or not :-(
     
    Timothy Hunter, Aug 2, 2006
    #3
  4. Timothy Hunter

    Guest

    On Thu, 3 Aug 2006, Timothy Hunter wrote:

    > wrote:
    >> jib:~ > ruby -r RMagick -e' p
    >> Magick::Image.instance_methods.grep(/raise/) '
    >> ["raise"]
    >>
    >>
    >> btw, seems like you could get away with something like:
    >>
    >> pct = Float(pct) rescue (Float(pct.to_s.delete('%'))/100)
    >> r = 0.0 .. 1.0
    >> pct = [r.min, pct].max
    >> pct = [r.max, pct].min
    >>
    >> cheers.
    >>
    >> -a

    > Arg! I should spend more time coding in Ruby and less in C. Thanks, Ara!


    yeah - i have the opposite issue ;-)

    > "1%0%".delete('%') => "10". Might be a bit too tolerent, don't you think?


    dunno. you could do pct.sub(/%$/,'') too. lately i've found that parameter
    checking just comes back to haunt me - though i do it heavily in certain
    situations. just food for thought.

    > I had the clamping down to 1 line but when I looked at it the next day I
    > couldn't tell if it was correct or not :-(


    indeed - i tend even to avoid method chaining in production code for that
    reason - it makes logs less than useful for pinpointng errors

    cheers.

    -a
    --
    we can never obtain peace in the outer world until we make peace with
    ourselves.
    - h.h. the 14th dali lama
     
    , Aug 2, 2006
    #4
  5. Timothy Hunter

    F. Senault Guest

    Le 2 août 2006 à 23:38, a écrit :

    > btw, seems like you could get away with something like:
    >
    > pct = Float(pct) rescue (Float(pct.to_s.delete('%'))/100)
    > r = 0.0 .. 1.0
    > pct = [r.min, pct].max
    > pct = [r.max, pct].min


    BTW, another fun way to write the three last lines would be :

    [0.0, pct, 1.0].sort[1]

    But it doesn't convey the meaning well... :)

    Fred
    --
    I bought me an illusion An' I put it on the wall
    I left it fill my head with dreams And I had to have them all
    But oh the taste is never so sweet As what you'b believe it is...
    Well I guess it never is (Guns n' Roses, Locomotive)
     
    F. Senault, Aug 2, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news
    Replies:
    2
    Views:
    1,117
  2. Une bévue

    can't convert String into Integer

    Une bévue, Feb 20, 2006, in forum: Ruby
    Replies:
    6
    Views:
    275
    Une bévue
    Feb 21, 2006
  3. aidy
    Replies:
    8
    Views:
    355
    Gregor Kopp
    May 5, 2006
  4. Randy Kramer
    Replies:
    12
    Views:
    384
    Robert Klemme
    Oct 25, 2007
  5. Une Bévue
    Replies:
    4
    Views:
    706
    Une Bévue
    May 28, 2010
Loading...

Share This Page