[#964] Bastion or SecurityManager for Ruby? — Clemens Hintze <clemens.hintze@...>

Hi,

15 messages 1999/12/10

[#989] a question about to_i — Friedrich Dominicus <Friedrich.Dominicus@...>

Sorry, I'm quite new to ruby. But I encounterd the following problem. If

17 messages 1999/12/19

[ruby-talk:00993] Re: a question about to_i

From: Yasushi Shoji <yashi@...>
Date: 1999-12-19 09:04:45 UTC
List: ruby-talk #993
From: Friedrich Dominicus <Friedrich.Dominicus@inka.de>
Subject: [ruby-talk:00991] Re: a question about to_i
Date: Sun, 19 Dec 1999 09:14:35 +0100

> Now that is not a reason for me what about
> print s.to_i * t.to_i + u, "\n" then it will give me 0 anyway, but that
> won't help much and

how about

print(s.to_i * t.to_i + u, "\n") unless s.to_i != 0

> what about print t.to_i / s.to_i? 

begin
  print t.to_i / s.to_i, "\n"
rescue
  print "can't divide by zero\n"
end

or

print (t.to_i / (s.to_i == 0 ? 1 : s.to_i))

even thought you are dealing with just numbers, i guess you should
check divisor, anyway.
 
> I need this check not in my example but I found it scary to just accept
> that any String will turn into integer zero. 

ok, i found it in archive.  it's in Japanse but i think someone is
working on a project which translate archives to english.

regards,
--
         yashi


class String
  def to_float
    return to_f if self =~ /^[-+]?((\d+)?(\.\d+))|((\d+)(\.\d+)?)([eE][-+]\d+)?$/
    raise "fail at convert string to float"
  end
  def to_int
    return to_i if self =~ /^[-+]?(\d+)$/
    raise "fail at convert string to int"
  end
end

def c x
    begin
      x.to_s.to_float
      begin
        x.to_s.to_int
        t = "int"
      rescue
        t = "float"
      end
    rescue
      t = "string"
    end
end


def checkall ar
  ar.each do |x|
    t = c(x)
    print t, " ", x, "\n"
  end
end

ar =  []
ar << "abc"
ar << "123"
ar << "1.23"
ar << "1.23e+4"
ar << ".123"
ar << "1e2"
checkall ar

In This Thread