Matrix-element extractor -- How to define it

Discussion in 'Ruby' started by RichardOnRails, May 3, 2008.

  1. Hi,

    I defined and populated a 9x9 matrix (Array of Arrays) of strings, m

    I defined an element-extractor:

    class Array
    def get(i,j)
    line =self[i-1]
    char = line[j-1]
    end
    end

    To get the first element of the second row, I used the following,
    which worked fine:

    m.get(2,1)

    I'd like to write merely m(2,1). How can I do that simply?

    Thanks in Advance,
    Richard
     
    RichardOnRails, May 3, 2008
    #1
    1. Advertising

  2. Hi,

    You do this:
    class Array
    alias old_[] []
    def [] *args
    if *args.length == 2 && args[0].is_a? Integer && args[1].is_a? Integer
    get(*args)
    else
    old_[](*args)
    end
    end

    Of course, this would remove some of the functionality of an Array for
    every Array, not just matricies. Consider making a subclass of Array
    like this:
    class Matrix < Array
    ...
    end

    Now you can modify Matrix at will without affecting Array.

    If you are creating a Matrix for purely pragmatic reasons (i.e., not
    as an excercise) then there is an excellent Matrix class in the Ruby
    standard library: http://ruby-doc.org/stdlib/

    Dan

    On Sat, May 3, 2008 at 4:05 PM, RichardOnRails
    <> wrote:
    > Hi,
    >
    > I defined and populated a 9x9 matrix (Array of Arrays) of strings, m
    >
    > I defined an element-extractor:
    >
    > class Array
    > def get(i,j)
    > line =self[i-1]
    > char = line[j-1]
    > end
    > end
    >
    > To get the first element of the second row, I used the following,
    > which worked fine:
    >
    > m.get(2,1)
    >
    > I'd like to write merely m(2,1). How can I do that simply?
    >
    > Thanks in Advance,
    > Richard
    >
    >
     
    Daniel Finnie, May 4, 2008
    #2
    1. Advertising

  3. On May 3, 9:06 pm, Daniel Finnie <> wrote:
    > Hi,
    >
    > You do this:
    > class Array
    > alias old_[] []
    > def [] *args
    > if *args.length == 2 && args[0].is_a? Integer && args[1].is_a? Integer
    > get(*args)
    > else
    > old_[](*args)
    > end
    > end
    >
    > Of course, this would remove some of the functionality of an Array for
    > every Array, not just matricies. Consider making a subclass of Array
    > like this:
    > class Matrix < Array
    > ...
    > end
    >
    > Now you can modify Matrix at will without affecting Array.
    >
    > If you are creating a Matrix for purely pragmatic reasons (i.e., not
    > as an excercise) then there is an excellent Matrix class in the Ruby
    > standard library:http://ruby-doc.org/stdlib/
    >
    > Dan
    >
    > On Sat, May 3, 2008 at 4:05 PM, RichardOnRails
    >
    > <> wrote:
    > > Hi,

    >
    > > I defined and populated a 9x9 matrix (Array of Arrays) of strings, m

    >
    > > I defined an element-extractor:

    >
    > > class Array
    > > def get(i,j)
    > > line =self[i-1]
    > > char = line[j-1]
    > > end
    > > end

    >
    > > To get the first element of the second row, I used the following,
    > > which worked fine:

    >
    > > m.get(2,1)

    >
    > > I'd like to write merely m(2,1). How can I do that simply?

    >
    > > Thanks in Advance,
    > > Richard


    Hi Daniel,

    Thanks for the "kick start" and the Matrix class. I got the latter
    working, but I'm going to "roll my own" because I want a Sudoku
    solver that works the way I do manually. I know there's a free solver
    written in VisualProlog, but like Frank Sinatra, "I'll do it my
    way" :)

    > alias old_[] []


    I'm running ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]. It
    barfed on "old_", so I substituted:
    else
    super(*args)
    which worked fine.

    Best wishes,
    Richard
     
    RichardOnRails, May 4, 2008
    #3
  4. On May 4, 4:38 pm, RichardOnRails
    <> wrote:
    > On May 3, 9:06 pm, Daniel Finnie <> wrote:
    >
    >
    >
    > > Hi,

    >
    > > You do this:
    > > class Array
    > > alias old_[] []
    > > def [] *args
    > > if *args.length == 2 && args[0].is_a? Integer && args[1].is_a? Integer
    > > get(*args)
    > > else
    > > old_[](*args)
    > > end
    > > end

    >
    > > Of course, this would remove some of the functionality of an Array for
    > > every Array, not just matricies. Consider making a subclass of Array
    > > like this:
    > > class Matrix < Array
    > > ...
    > > end

    >
    > > Now you can modify Matrix at will without affecting Array.

    >
    > > If you are creating a Matrix for purely pragmatic reasons (i.e., not
    > > as an excercise) then there is an excellent Matrix class in the Ruby
    > > standard library:http://ruby-doc.org/stdlib/

    >
    > > Dan

    >
    > > On Sat, May 3, 2008 at 4:05 PM, RichardOnRails

    >
    > > <> wrote:
    > > > Hi,

    >
    > > > I defined and populated a 9x9 matrix (Array of Arrays) of strings, m

    >
    > > > I defined an element-extractor:

    >
    > > > class Array
    > > > def get(i,j)
    > > > line =self[i-1]
    > > > char = line[j-1]
    > > > end
    > > > end

    >
    > > > To get the first element of the second row, I used the following,
    > > > which worked fine:

    >
    > > > m.get(2,1)

    >
    > > > I'd like to write merely m(2,1). How can I do that simply?

    >
    > > > Thanks in Advance,
    > > > Richard

    >
    > Hi Daniel,
    >
    > Thanks for the "kick start" and the Matrix class. I got the latter
    > working, but I'm going to "roll my own" because I want a Sudoku
    > solver that works the way I do manually. I know there's a free solver
    > written in VisualProlog, but like Frank Sinatra, "I'll do it my
    > way" :)
    >
    > > alias old_[] []

    >
    > I'm running ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]. It
    > barfed on "old_", so I substituted:
    > else
    > super(*args)
    > which worked fine.
    >
    > Best wishes,
    > Richard


    I should have mentioned that I used "alias old_[] [] " in the context
    of "Matrix < Array", so there's no wonder it didn't work. Sorry about
    that omission.
     
    RichardOnRails, May 4, 2008
    #4
  5. Hi again,

    I'm glad super worked out for you. That's the "right way" to do it
    for inheritance.

    I have a feeling I may have misguided you with the "alias old_[] []"
    line. It should probably be something like "alias old_brackets []" as
    I doubt that old_[] is a valid method name (I just checked and it
    isn't, to be sure).

    Dan

    On Sun, May 4, 2008 at 4:40 PM, RichardOnRails
    <> wrote:
    > On May 3, 9:06 pm, Daniel Finnie <> wrote:
    > > Hi,
    > >
    > > You do this:
    > > class Array
    > > alias old_[] []
    > > def [] *args
    > > if *args.length == 2 && args[0].is_a? Integer && args[1].is_a? Integer
    > > get(*args)
    > > else
    > > old_[](*args)
    > > end
    > > end
    > >
    > > Of course, this would remove some of the functionality of an Array for
    > > every Array, not just matricies. Consider making a subclass of Array
    > > like this:
    > > class Matrix < Array
    > > ...
    > > end
    > >
    > > Now you can modify Matrix at will without affecting Array.
    > >
    > > If you are creating a Matrix for purely pragmatic reasons (i.e., not
    > > as an excercise) then there is an excellent Matrix class in the Ruby
    > > standard library:http://ruby-doc.org/stdlib/
    > >
    > > Dan
    > >
    > > On Sat, May 3, 2008 at 4:05 PM, RichardOnRails
    > >

    >
    > > <> wrote:
    > > > Hi,

    > >
    > > > I defined and populated a 9x9 matrix (Array of Arrays) of strings, m

    > >
    > > > I defined an element-extractor:

    > >
    > > > class Array
    > > > def get(i,j)
    > > > line =self[i-1]
    > > > char = line[j-1]
    > > > end
    > > > end

    > >
    > > > To get the first element of the second row, I used the following,
    > > > which worked fine:

    > >
    > > > m.get(2,1)

    > >
    > > > I'd like to write merely m(2,1). How can I do that simply?

    > >
    > > > Thanks in Advance,
    > > > Richard

    >
    > Hi Daniel,
    >
    > Thanks for the "kick start" and the Matrix class. I got the latter
    > working, but I'm going to "roll my own" because I want a Sudoku
    > solver that works the way I do manually. I know there's a free solver
    > written in VisualProlog, but like Frank Sinatra, "I'll do it my
    > way" :)
    >
    > > alias old_[] []

    >
    > I'm running ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]. It
    > barfed on "old_", so I substituted:
    > else
    > super(*args)
    > which worked fine.
    >
    > Best wishes,
    > Richard
    >
    >
    >
     
    Daniel Finnie, May 4, 2008
    #5
  6. On May 4, 5:44 pm, Daniel Finnie <> wrote:
    > Hi again,
    >
    > I'm glad super worked out for you. That's the "right way" to do it
    > for inheritance.
    >
    > I have a feeling I may have misguided you with the "alias old_[] []"
    > line. It should probably be something like "alias old_brackets []" as
    > I doubt that old_[] is a valid method name (I just checked and it
    > isn't, to be sure).
    >
    > Dan
    >
    > On Sun, May 4, 2008 at 4:40 PM, RichardOnRails
    >
    > <> wrote:
    > > On May 3, 9:06 pm, Daniel Finnie <> wrote:
    > > > Hi,

    >
    > > > You do this:
    > > > class Array
    > > > alias old_[] []
    > > > def [] *args
    > > > if *args.length == 2 && args[0].is_a? Integer && args[1].is_a? Integer
    > > > get(*args)
    > > > else
    > > > old_[](*args)
    > > > end
    > > > end

    >
    > > > Of course, this would remove some of the functionality of an Array for
    > > > every Array, not just matricies. Consider making a subclass of Array
    > > > like this:
    > > > class Matrix < Array
    > > > ...
    > > > end

    >
    > > > Now you can modify Matrix at will without affecting Array.

    >
    > > > If you are creating a Matrix for purely pragmatic reasons (i.e., not
    > > > as an excercise) then there is an excellent Matrix class in the Ruby
    > > > standard library:http://ruby-doc.org/stdlib/

    >
    > > > Dan

    >
    > > > On Sat, May 3, 2008 at 4:05 PM, RichardOnRails

    >
    > > > <> wrote:
    > > > > Hi,

    >
    > > > > I defined and populated a 9x9 matrix (Array of Arrays) of strings, m

    >
    > > > > I defined an element-extractor:

    >
    > > > > class Array
    > > > > def get(i,j)
    > > > > line =self[i-1]
    > > > > char = line[j-1]
    > > > > end
    > > > > end

    >
    > > > > To get the first element of the second row, I used the following,
    > > > > which worked fine:

    >
    > > > > m.get(2,1)

    >
    > > > > I'd like to write merely m(2,1). How can I do that simply?

    >
    > > > > Thanks in Advance,
    > > > > Richard

    >
    > > Hi Daniel,

    >
    > > Thanks for the "kick start" and the Matrix class. I got the latter
    > > working, but I'm going to "roll my own" because I want a Sudoku
    > > solver that works the way I do manually. I know there's a free solver
    > > written in VisualProlog, but like Frank Sinatra, "I'll do it my
    > > way" :)

    >
    > > > alias old_[] []

    >
    > > I'm running ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]. It
    > > barfed on "old_", so I substituted:
    > > else
    > > super(*args)
    > > which worked fine.

    >
    > > Best wishes,
    > > Richard


    Hi Daniel,

    Thanks for your additional response.

    > misguided


    No problem! I misguide myself all the time :) Any, it's history.

    I've got two questions about my current version of your code, which
    works fine BTW.

    1. Where is the get method come from. I looked in Ruby RDoc and (i)
    see no get method for Array, nor (ii) any related get method on the
    Methods section of the RDoc. I'd really like a pointer to it's
    documentation. It certainly is useful in this context.

    2. As I mentioned, I switched from "old_[](*args)" in the else
    clause to "super *args". which works fine for the most part. I works
    fine, too, except I want to substitute "raise 'invalid data'" or
    something more descriptive. Unfortunately, when I do that I get a
    syntax error.

    If you have the time, I'd love to get your take on these issues.
    Code and output is below for your convenience.

    Best wishes,
    Richard

    class Matrix < Array
    def [] *args
    if (args.length == 2) && args[0].is_a?(Integer) && args[1].is_a?
    (Integer)
    get(*args)
    else
    super *args
    end
    end
    end

    m = Matrix[ [10,20,30], [40,50,60], [70,80,90] ];
    puts m [0] [1] # 20
    puts m [2] [0] # 70
    puts m[1] # 40
    # 50
    # 60

    # This displayed nicely in my browser (Firefox 2.0). I hope it does
    in yours!
     
    RichardOnRails, May 5, 2008
    #6
    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. =?Utf-8?B?VmlqYXk=?=

    Web Extractor

    =?Utf-8?B?VmlqYXk=?=, Apr 8, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    368
    =?Utf-8?B?VmlqYXk=?=
    Apr 8, 2004
  2. Luigi Donatello Asero

    Semantics extractor

    Luigi Donatello Asero, Feb 14, 2004, in forum: HTML
    Replies:
    5
    Views:
    564
    Eric Bohlman
    Feb 14, 2004
  3. laikon
    Replies:
    3
    Views:
    364
    James Kanze
    Oct 8, 2008
  4. Terry Reedy
    Replies:
    0
    Views:
    588
    Terry Reedy
    Apr 2, 2009
  5. Robert Kern
    Replies:
    0
    Views:
    626
    Robert Kern
    Apr 2, 2009
Loading...

Share This Page