Integer("This is not a number")
ArgumentError: invalid value for Integer(): "This is not a number"
One gotcha: If you provide a zero at the front, by default it'll try
parsing it as a Octal, Hexadecimal or Binary as appropriate:
# Why programmers get Halloween mixed up with Christmas
Integer("031") == 25
=> true
On recent versions of Ruby, you can get around that by specifying the base:
Integer("031",10)
=> 31
It's documented at
http://ruby-doc.org/core/classes/Kernel.html#M001434 , and you can
also read about other methods such as Array(), Float() and String().
Another nice piece of honey badgering is Array#fetch and Hash#fetch.
Rather than timidly handing you nil if it can't do what you want and
hoping nothing bad will happen in the short term, it'll raise an
exception by default.
bottle_o = {}
bottle_o.fetch(:beer)
KeyError: key not found: :beer
You can set a default if it can't find something, or you can run a custom block
bottle_o.fetch(:beer, :water) # Returns water if there's no beer
# May not be valid Japanese -
http://japanese.stackexchange.com/questions/3113/is-it-unusual-to-have-the-ga-particle-with-a-negative-verb
bottle_o.fetch(:beer) {|key| raise "sakaya ni biru ga arimasen"}
If you need further printf debugging (puts debugging?), you may want
to inspect what keys actually exist:
Sake, Coke = Array.new(2) { Class.new } # Don't you guys know that
classes are just objects of class Class?
bottle_o = {:nihonshu => Sake.new, :kokakorakurashikku => Coke.new}
bottle_o.fetch(:beer) {|key| raise "We don't have #{key.inspect}, but
we have #{bottle_o.keys.inspect}" }
The method ... method looks like the following:
# Incidentally, I initially had "3.1" - Integer firmly reminded me
that 3.1 isn't an integer!
strings = ["31", "42"]
numbers = strings.map(&method(:Integer))
the method ... method takes a symbol, and returns a Method object.
Method has a to_proc method, and the ampersand tells it to supply the
proc, which can be used by methods that take a block. RDoc:
http://ruby-doc.org/core/classes/Object.html#M001038 and
http://ruby-doc.org/core/classes/Method.html#M001069
I came across this notation from Jorg W Mittag's answer
http://stackoverflow.com/questions/6962883/how-to-unflatten-a-ruby-array/6963422#6963422
. He is the Jon Skeet of Ruby:
http://stackoverflow.com/tags/ruby/topusers
I ought to write about &method in a blog post and advertise it on RubyFlow.
Andrew
I ran into exactly the same problem with Javascript a couple of days ago
parseInt("09");
=> 0
parseInt("09", 10);
=> 9
Malc
Samuel Richardson
www.richardson.co.nz | 0405 472 748
> --
> You received this message because you are subscribed to the Google Groups "Ruby or Rails Oceania" group.
> To post to this group, send email to rails-...@googlegroups.com.
> To unsubscribe from this group, send email to rails-oceani...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/rails-oceania?hl=en.
>
>
Agreed. 'Potential gotcha' would be a better description.
Malc