Ampersand method honey_badger

16 views
Skip to first unread message

Andrew Grimm

unread,
Sep 13, 2011, 9:40:39 AM9/13/11
to rails-...@googlegroups.com
The Integer() method doesn't take no ****. If you give it something
that's not a number, it'll tell you up front by throwing an exception

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

Malcolm Locke

unread,
Sep 13, 2011, 6:36:51 PM9/13/11
to rails-...@googlegroups.com
On Tue, Sep 13, 2011 at 11:40:39PM +1000, Andrew Grimm wrote:
> On recent versions of Ruby, you can get around that by specifying the base:
>
> Integer("031",10)
> => 31

I ran into exactly the same problem with Javascript a couple of days ago

parseInt("09");
=> 0
parseInt("09", 10);
=> 9

Malc

Samuel Richardson

unread,
Sep 13, 2011, 7:11:14 PM9/13/11
to rails-...@googlegroups.com
Always pass the base/radix when calling parseInt. I think jLint will
pick this up with a warning as well if you're using it.

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.
>
>

Aleksey Gureiev

unread,
Sep 14, 2011, 12:24:16 PM9/14/11
to rails-...@googlegroups.com
Not sure it's a "problem". If you prepend a number with "0" is a 8-base number. Always was and always will be.

- Aleksey

Aleksey Gureiev

unread,
Sep 14, 2011, 12:27:15 PM9/14/11
to rails-...@googlegroups.com
Thanks for the Array.new(2) { ... } tip, Andrew. Previously I did:

2.times.map { |n| ... }

It's probably still a valid approach, but it's good to know there's an alternative.

- Aleksey

Malcolm Locke

unread,
Sep 14, 2011, 6:07:59 PM9/14/11
to rails-...@googlegroups.com
On Wed, Sep 14, 2011 at 09:24:16AM -0700, Aleksey Gureiev wrote:
> Not sure it's a "problem". If you prepend a number with "0" is a 8-base
> number. Always was and always will be.

Agreed. 'Potential gotcha' would be a better description.

Malc

Reply all
Reply to author
Forward
0 new messages