Any way to get lists to throw exceptions on incorrect access

I’m slowly doing more in Ruby (in addition to what I do in Python), as I
like the cleanliness of the language. However, one thing that I really
don’t like is the fact that an attempt to access a non-existent list
item silently returns nil, rather than throwing an exception. Is there a
way to make this more strict? Hashes have the same behavior, but since
they can be assigned a default block, that isn’t nearly so much of a
problem.

I know I could change List behavior, but there are very obvious reasons
why that’s a Bad Thing. I could also subclass list, but then I miss all
of the syntactic niceties of lists. And both solutions impose an extra
level of calling for what will be very frequent actions.

Any thoughts welcome,
Ken

Kenneth McDonald wrote:

miss all of the syntactic niceties of lists. And both solutions impose
an extra level of calling for what will be very frequent actions.

Array#fetch will raise an exception if the index does not exist.

Jamey

Thanks. Too bad I can’t use the [] indexing notation, though.

I’m starting to learn the Ruby libraries…

Ken

On Oct 12, 11:07 pm, Kenneth McDonald
[email protected] wrote:

really don’t like is the fact that an attempt to access a
Array#fetch will raise an exception if the index does not exist.

Jamey

Set the default proc to raise an exception…

a=Hash.new {raise ArgumentError}
a[1] #=> raises an ArgumentError

_Kevin

Yep, got that one. Unfortunately, there’s no equivalent for lists.
Actually, I’m going to make another post on this in the next few
minutes.

On 2006.10.13 09:11, Kenneth McDonald wrote:

of the syntactic niceties of lists. And both solutions impose an extra
level of calling for what will be very frequent actions.

Any thoughts welcome,

Since you asked… :slight_smile:

We have managed to make do without Array access raising exceptions.

I would REALLY recommend leaving behind the baggage of any previous
languages and take a head-first plunge. In Ruby, Array access returns
a nil if the index is not found. Embrace it! Form your code around the
Ruby idioms, not the other way around!

The payoff will be much greater.

Eero S. wrote:

Certainly possible. I’m not uncomfortable with new ways of doing things
(I really like Ruby iteration, for example), I’m just worried this
difference might cause hard-to-find errors. But I just posted that as a
more general question, so 'nuff said here :slight_smile:

On Fri, 13 Oct 2006, Kenneth McDonald wrote:

The payoff will be much greater.

Certainly possible. I’m not uncomfortable with new ways of doing things (I
really like Ruby iteration, for example), I’m just worried this difference
might cause hard-to-find errors. But I just posted that as a more general
question, so 'nuff said here :slight_smile:

it’s not that hard if you want, and you only need modify the array’s you
desire

  • not all of them in you program:

    harp:~ > cat a.rb
    class Array
    module BoundsCheck
    def [] a, *b
    b = b.shift
    max =
    case a
    when Numeric
    a
    when Range
    a.exclude_end? ? (a.end-1) : e.end
    else
    b
    end
    raise IndexError, max.to_s if max > (size - 1)
    super
    end
    end

    def bounds_check!
    extend BoundsCheck
    end
    end

    array = %w[ 0 1 2 ]

    4.times{|i| puts “array[#{ i }] : #{ array[i].inspect }”}

    array.bounds_check!

    4.times{|i| puts “array[#{ i }] : #{ array[i].inspect }”}

    harp:~ > ruby a.rb
    array[0] : “0”
    array[1] : “1”
    array[2] : “2”
    array[3] : nil
    array[0] : “0”
    array[1] : “1”
    array[2] : “2”
    a.rb:15:in `[]’: 3 (IndexError)
    from a.rb:31
    from a.rb:31

to be robust you’d want to wrap Array#slice and other ‘index-y’ methods.

regards.

-a

James Edward G. II wrote:

    from :0

hash.fetch(:missing)
IndexError: key not found
from (irb):3:in `fetch’
from (irb):3
from :0

James Edward G. II

Sorry, shoulda phrased more clearly; there’s no solution that checks
indexing on all the various list ops that uses indexing.

It doesn’t really matter, from what’s been said, this doesn’t seem to be
a major issue when programming in Ruby.

On Oct 12, 2006, at 11:26 PM, Kenneth McDonald wrote:

Yep, got that one. Unfortunately, there’s no equivalent for lists.

array, hash = Array.new, Hash.new
=> [[], {}]

array.fetch(999)
IndexError: index 999 out of array
from (irb):2:in `fetch’
from (irb):2
from :0

hash.fetch(:missing)
IndexError: key not found
from (irb):3:in `fetch’
from (irb):3
from :0

James Edward G. II

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs