Helper to create multi-dimensional arrays

Discussion in 'Ruby' started by Anthony Martinez, Jun 24, 2007.

  1. --kr14OxHsRwZHHqxS
    Content-Type: text/plain; charset=us-ascii
    Content-Disposition: inline
    Content-Transfer-Encoding: quoted-printable

    I came up with this method really quick to create x*y arrays in Ruby. It
    mimicks Array#new's behavior pretty closely wrt blocks, I hope. What do you=
    all
    think?

    class << Array
    def multi(x,y,*args, &block)
    if args.length > 0 and block_given?
    raise ArgumentError, "wrong number of arguments (#{args.length =
    + 2} for 2)"
    elsif args.length > 1 and not block_given?
    raise ArgumentError, "wrong number of arguments (#{args.length =
    + 2} for 3)"
    end

    Array.new(x) do
    if block_given?
    Array.new(y, &block)
    else
    Array.new(y, args[0])
    end
    end
    end
    end

    Usage is:

    Array.multi(5,5,0) =20
    # =3D> [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0],=
    [0, 0, 0, 0, 0]]
    Array.multi(5,5) {""}
    # =3D> [["", "", "", "", ""], ["", "", "", "", ""], ["", "", "", "", ""], [=
    "", "", "", "", ""], ["", "", "", "", ""]]

    --=20
    panic ("No CPUs found. System halted.\n");
    2.4.3 linux/arch/parisc/kernel/setup.c

    --kr14OxHsRwZHHqxS
    Content-Type: application/pgp-signature
    Content-Disposition: inline

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.6 (GNU/Linux)

    iD8DBQFGfr77KaiGM/xGKzQRApS2AJoD7vFPvqOjYJs2DShP5FFJPjTPDwCgoy5c
    RKfZw/CmHzE9o1AhY3tfz8Q=
    =GE3k
    -----END PGP SIGNATURE-----

    --kr14OxHsRwZHHqxS--
     
    Anthony Martinez, Jun 24, 2007
    #1
    1. Advertising

  2. On 6/24/07, Anthony Martinez <> wrote:

    > Usage is:
    >
    > Array.multi(5,5,0)
    > # => [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
    > Array.multi(5,5) {""}
    > # => [["", "", "", "", ""], ["", "", "", "", ""], ["", "", "", "", ""], ["", "", "", "", ""], ["", "", "", "", ""]]


    I'm far too lazy.

    I'd just do

    a = [[0]*5]]*5
     
    Gregory Brown, Jun 24, 2007
    #2
    1. Advertising

  3. On 6/24/07, Gregory Brown <> wrote:
    > On 6/24/07, Anthony Martinez <> wrote:
    >
    > > Usage is:
    > >
    > > Array.multi(5,5,0)
    > > # => [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
    > > Array.multi(5,5) {""}
    > > # => [["", "", "", "", ""], ["", "", "", "", ""], ["", "", "", "", ""], ["", "", "", "", ""], ["", "", "", "", ""]]

    >
    > I'm far too lazy.
    >
    > I'd just do
    >
    > a = [[0]*5]]*5


    Arg. don't do that. it makes the same array for each. :-/
     
    Gregory Brown, Jun 24, 2007
    #3
  4. Ruby Debugger

    Hello everybody,

    Ruby nuby here.
    I'm doing my best to sort out problems for myself but this one has me stumped.
    I decided to try out the Ruby debugger after reading about it in the
    Pickaxe, but when I
    try to list the script-to-be-debugged, I always get the same output -
    i.e. from ubygems.rb
    as shown below - (Ch4_1.rb is just a tiny script from Chris Pine's
    _Learn to Program_ ):

    C:\ruby\usr\LtP>ruby -d -rdebug Ch4_1.rb
    Debug.rb
    Emacs support available.

    c:/ruby/lib/ruby/site_ruby/1.8/ubygems.rb:10:require 'rubygems'
    (rdb:1) list 1-9
    [1, 9] in c:/ruby/lib/ruby/site_ruby/1.8/ubygems.rb
    1 # This file allows for the running of rubygems with a nice
    2 # command line look-and-feel: ruby -rubygems foo.rb
    3 #--
    4 # Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
    5 # All rights reserved.
    6 # See LICENSE.txt for permissions.
    7 #++
    8
    9

    It does'nt matter what script I take as input, the result is always the same.
    Can anyone please tell me what I'm doing wrong, based in this?
    Regards,
    Dick Summerfield
    Eindhoven,
    Netherlands.



    --
    No virus found in this outgoing message.
    Checked by AVG Free Edition.
    Version: 7.5.472 / Virus Database: 269.9.6/863 - Release Date: 23/06/2007 11:08
     
    Dick Summerfield, Jun 24, 2007
    #4
  5. Anthony Martinez

    Jason Vinson Guest

    Re: Ruby Debugger

    What I tend to do is load the debugger, then set a breakpoint at the line in
    my code I want to debug:

    > ruby -r debug my_file.rb

    Debug.rb
    Emacs support available.

    C:/ruby-1.8.6/lib/ruby/site_ruby/1.8/ubygems.rb:10:require 'rubygems'
    (rdb:1) b my_file.rb:7
    Set breakpoint 1 at mongrel_proxy.rb:7
    (rdb:1) c

    This would set up my code to hit a breakpoint at line #7.

    HTH,
    Jason

    -----Original Message-----
    From: Dick Summerfield [mailto:]
    Sent: Sunday, June 24, 2007 4:00 PM
    To: ruby-talk ML
    Subject: Ruby Debugger

    Hello everybody,

    Ruby nuby here.
    I'm doing my best to sort out problems for myself but this one has me
    stumped.
    I decided to try out the Ruby debugger after reading about it in the
    Pickaxe, but when I
    try to list the script-to-be-debugged, I always get the same output -
    i.e. from ubygems.rb
    as shown below - (Ch4_1.rb is just a tiny script from Chris Pine's
    _Learn to Program_ ):

    C:\ruby\usr\LtP>ruby -d -rdebug Ch4_1.rb
    Debug.rb
    Emacs support available.

    c:/ruby/lib/ruby/site_ruby/1.8/ubygems.rb:10:require 'rubygems'
    (rdb:1) list 1-9
    [1, 9] in c:/ruby/lib/ruby/site_ruby/1.8/ubygems.rb
    1 # This file allows for the running of rubygems with a nice
    2 # command line look-and-feel: ruby -rubygems foo.rb
    3 #--
    4 # Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
    5 # All rights reserved.
    6 # See LICENSE.txt for permissions.
    7 #++
    8
    9

    It does'nt matter what script I take as input, the result is always the
    same.
    Can anyone please tell me what I'm doing wrong, based in this?
    Regards,
    Dick Summerfield
    Eindhoven,
    Netherlands.



    --
    No virus found in this outgoing message.
    Checked by AVG Free Edition.
    Version: 7.5.472 / Virus Database: 269.9.6/863 - Release Date: 23/06/2007
    11:08
     
    Jason Vinson, Jun 24, 2007
    #5
  6. Anthony Martinez

    Jason Vinson Guest

    Re: Ruby Debugger

    Btw, you are most likely seeing this b/c you have "-rubygems" in your
    RUBYOPT env.

    Jason

    -----Original Message-----
    From: Dick Summerfield [mailto:]
    Sent: Sunday, June 24, 2007 4:00 PM
    To: ruby-talk ML
    Subject: Ruby Debugger

    Hello everybody,

    Ruby nuby here.
    I'm doing my best to sort out problems for myself but this one has me
    stumped.
    I decided to try out the Ruby debugger after reading about it in the
    Pickaxe, but when I
    try to list the script-to-be-debugged, I always get the same output -
    i.e. from ubygems.rb
    as shown below - (Ch4_1.rb is just a tiny script from Chris Pine's
    _Learn to Program_ ):

    C:\ruby\usr\LtP>ruby -d -rdebug Ch4_1.rb
    Debug.rb
    Emacs support available.

    c:/ruby/lib/ruby/site_ruby/1.8/ubygems.rb:10:require 'rubygems'
    (rdb:1) list 1-9
    [1, 9] in c:/ruby/lib/ruby/site_ruby/1.8/ubygems.rb
    1 # This file allows for the running of rubygems with a nice
    2 # command line look-and-feel: ruby -rubygems foo.rb
    3 #--
    4 # Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
    5 # All rights reserved.
    6 # See LICENSE.txt for permissions.
    7 #++
    8
    9

    It does'nt matter what script I take as input, the result is always the
    same.
    Can anyone please tell me what I'm doing wrong, based in this?
    Regards,
    Dick Summerfield
    Eindhoven,
    Netherlands.



    --
    No virus found in this outgoing message.
    Checked by AVG Free Edition.
    Version: 7.5.472 / Virus Database: 269.9.6/863 - Release Date: 23/06/2007
    11:08
     
    Jason Vinson, Jun 24, 2007
    #6
  7. Re: Ruby Debugger

    Thanks Jason, that definitely helps.
    I haven't been able to get it to work yet when rubygems is loaded
    (but will keep experimenting).
    However when I remove "RUBYOPT= -rubygems" from the environment the
    debugger works just like the book
    says it should :).

    That should be "books" because apart from PA there is a "Debugging
    Ruby programs 101" from IBM,
    but neither mention the effect of the RUBYOPT environment parameter.
    Too UNIX oriented, perhaps??

    Thanks,

    Dick.

    At 22:09 24/06/2007, you wrote:

    >What I tend to do is load the debugger, then set a breakpoint at the line in
    >my code I want to debug:
    >
    > > ruby -r debug my_file.rb

    >Debug.rb
    >Emacs support available.
    >
    >C:/ruby-1.8.6/lib/ruby/site_ruby/1.8/ubygems.rb:10:require 'rubygems'
    >(rdb:1) b my_file.rb:7
    >Set breakpoint 1 at mongrel_proxy.rb:7
    >(rdb:1) c
    >
    >This would set up my code to hit a breakpoint at line #7.
    >
    >HTH,
    >Jason
    >
    >-----Original Message-----
    >From: Dick Summerfield [mailto:]
    >Sent: Sunday, June 24, 2007 4:00 PM
    >To: ruby-talk ML
    >Subject: Ruby Debugger
    >
    >Hello everybody,
    >
    >Ruby nuby here.
    >I'm doing my best to sort out problems for myself but this one has me
    >stumped.
    >I decided to try out the Ruby debugger after reading about it in the
    >Pickaxe, but when I
    >try to list the script-to-be-debugged, I always get the same output -
    >i.e. from ubygems.rb
    >as shown below - (Ch4_1.rb is just a tiny script from Chris Pine's
    >_Learn to Program_ ):
    >
    >C:\ruby\usr\LtP>ruby -d -rdebug Ch4_1.rb
    >Debug.rb
    >Emacs support available.
    >
    >c:/ruby/lib/ruby/site_ruby/1.8/ubygems.rb:10:require 'rubygems'
    >(rdb:1) list 1-9
    >[1, 9] in c:/ruby/lib/ruby/site_ruby/1.8/ubygems.rb
    > 1 # This file allows for the running of rubygems with a nice
    > 2 # command line look-and-feel: ruby -rubygems foo.rb
    > 3 #--
    > 4 # Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
    > 5 # All rights reserved.
    > 6 # See LICENSE.txt for permissions.
    > 7 #++
    > 8
    > 9
    >
    >It does'nt matter what script I take as input, the result is always the
    >same.
    >Can anyone please tell me what I'm doing wrong, based in this?
    >Regards,
    >Dick Summerfield
    >Eindhoven,
    >Netherlands.
    >
    >
    >
    >--
    >No virus found in this outgoing message.
    >Checked by AVG Free Edition.
    >Version: 7.5.472 / Virus Database: 269.9.6/863 - Release Date: 23/06/2007
    >11:08
    >
    >
    >
    >
    >
    >
    >
    >--
    >No virus found in this incoming message.
    >Checked by AVG Free Edition.
    >Version: 7.5.472 / Virus Database: 269.9.6/863 - Release Date:
    >23/06/2007 11:08



    --
    No virus found in this outgoing message.
    Checked by AVG Free Edition.
    Version: 7.5.472 / Virus Database: 269.9.6/863 - Release Date: 23/06/2007 11:08
     
    Dick Summerfield, Jun 24, 2007
    #7
  8. Anthony Martinez wrote:
    > I came up with this method really quick to create x*y arrays in Ruby. It
    > mimicks Array#new's behavior pretty closely wrt blocks, I hope. What do you all
    > think?


    What about making it more general than just x*y arrays?

    class << Array
    def multi(n, *args, &block)
    if args.empty?
    Array.new(n, &block)
    else
    Array.new(n) do
    Array.multi(*args, &block)
    end
    end
    end
    end

    ?> Array.multi(2){ 0 }
    => [0, 0]
    >> Array.multi(2,2){ 0 }

    => [[0, 0], [0, 0]]
    >> Array.multi(2,2,2){ 0 }

    => [[[0, 0], [0, 0]], [[0, 0], [0, 0]]]
    >> Array.multi(2,2,2,2){ 0 }




    hehe, playing around is fun

    Daniel
     
    Daniel DeLorme, Jun 25, 2007
    #8
  9. Re: Ruby Debugger

    Does anyone know of a gui ruby debugger for OS X?

    --Colin
     
    Colin Summers, Jun 25, 2007
    #9
  10. Re: Ruby Debugger

    Colin Summers wrote:
    > Does anyone know of a gui ruby debugger for OS X?
    >
    > --Colin
    >
    >

    Not sure this is strictly a "GUI Debugger" but Komodo does run on OS X
    and has a "typical IDE debugger interface" to Ruby.
     
    M. Edward (Ed) Borasky, Jun 25, 2007
    #10
  11. --UlsYxwg8UDQn+EKZ
    Content-Type: text/plain; charset=us-ascii
    Content-Disposition: inline
    Content-Transfer-Encoding: quoted-printable

    On Mon, Jun 25, 2007 at 08:06:44AM +0900, Daniel DeLorme wrote:
    > Anthony Martinez wrote:
    > >I came up with this method really quick to create x*y arrays in Ruby. It
    > >mimicks Array#new's behavior pretty closely wrt blocks, I hope. What do =

    you all
    > >think?

    >=20
    > What about making it more general than just x*y arrays?


    Ooh. That didn't occur to me. Nice.

    >=20
    > class << Array
    > def multi(n, *args, &block)
    > if args.empty?
    > Array.new(n, &block)
    > else
    > Array.new(n) do
    > Array.multi(*args, &block)
    > end
    > end
    > end
    > end
    >=20
    > ?> Array.multi(2){ 0 }
    > =3D> [0, 0]
    > >> Array.multi(2,2){ 0 }

    > =3D> [[0, 0], [0, 0]]
    > >> Array.multi(2,2,2){ 0 }

    > =3D> [[[0, 0], [0, 0]], [[0, 0], [0, 0]]]
    > >> Array.multi(2,2,2,2){ 0 }

    >=20
    >=20
    >=20
    > hehe, playing around is fun
    >=20
    > Daniel
    >=20


    --=20
    How'd you get this number?
    -- A Qwest Central Office Technician

    --UlsYxwg8UDQn+EKZ
    Content-Type: application/pgp-signature
    Content-Disposition: inline

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.6 (GNU/Linux)

    iD8DBQFGfwndKaiGM/xGKzQRAil1AJ9JI6MIaEqu6sWe15AOB+rSSgez8gCguGOc
    eHyT3a55RCoV15xSWxg9x0Y=
    =JFyL
    -----END PGP SIGNATURE-----

    --UlsYxwg8UDQn+EKZ--
     
    Anthony Martinez, Jun 25, 2007
    #11
  12. Re: Ruby Debugger

    Wow. $300. This must be a kickass debugger. I'll take a look, thanks.

    --Colin
     
    Colin Summers, Jun 25, 2007
    #12
  13. Martin Krauskopf, Jun 25, 2007
    #13
  14. Re: Ruby Debugger

    On Sunday 24 June 2007 16:31:00 Colin Summers wrote:
    > Does anyone know of a gui ruby debugger for OS X?
    >
    > --Colin


    Have you tried using eclipse and the ruby IDE built on it?

    david
     
    David Southwell, Jun 25, 2007
    #14
  15. Re: Ruby Debugger

    Martin Krauskopf wrote:
    > Colin Summers wrote:
    >> Does anyone know of a gui ruby debugger for OS X?

    >
    > Don't know how much of them run on OSx. I know that at least NetBeans
    > does ;)
    >
    > http://debug-commons.rubyforge.org/misc/ruby-debugging.html#gui
    >
    > m.
    >
    >
    >

    Does NetBeans run with the standard Ruby, or only with jRuby?
     
    M. Edward (Ed) Borasky, Jun 25, 2007
    #15
  16. Gregory Brown wrote:
    > I'm far too lazy.
    >
    > I'd just do
    >
    > a = [[0]*5]]*5


    But that does not quite allow the free use of method calls. e.g. dice
    rolling:

    p [[rand(6) + 1] * 4] * 4
    [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]

    p Array.multi(4, 4) {rand(6) + 1}
    [[6, 3, 1, 3], [4, 3, 1, 3], [1, 1, 5, 3], [6, 4, 2, 2]]

    --
    Posted via http://www.ruby-forum.com/.
     
    Lloyd Linklater, Jun 25, 2007
    #16
  17. On 6/25/07, Lloyd Linklater <> wrote:
    > Gregory Brown wrote:
    > > I'm far too lazy.
    > >
    > > I'd just do
    > >
    > > a = [[0]*5]]*5

    >
    > But that does not quite allow the free use of method calls. e.g. dice
    > rolling:
    >
    > p [[rand(6) + 1] * 4] * 4
    > [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]


    I posted again saying you shouldn't use the approach I mentioned
    because it creates copies of the same array.
     
    Gregory Brown, Jun 25, 2007
    #17
  18. Re: Ruby Debugger

    M. Edward (Ed) Borasky wrote:
    > Martin Krauskopf wrote:
    >> Colin Summers wrote:
    >>> Does anyone know of a gui ruby debugger for OS X?

    >> Don't know how much of them run on OSx. I know that at least NetBeans
    >> does ;)
    >>
    >> http://debug-commons.rubyforge.org/misc/ruby-debugging.html#gui
    >>
    >> m.
    >>
    >>
    >>

    > Does NetBeans run with the standard Ruby, or only with jRuby?
    >


    It runs with either. JRuby is set up by default, but there's a
    preferences pane where you can point at Ruby's binaries instead. Fast
    debugging (i.e. usable debugging, via ruby-debug) only works with Ruby
    at present (jruby-debug extension is coming soon).

    - Charlie
     
    Charles Oliver Nutter, Jun 25, 2007
    #18
  19. Re: Ruby Debugger

    M. Edward (Ed) Borasky wrote:
    > Martin Krauskopf wrote:
    >> Colin Summers wrote:
    >>> Does anyone know of a gui ruby debugger for OS X?

    >> Don't know how much of them run on OSx. I know that at least NetBeans
    >> does ;)
    >>
    >> http://debug-commons.rubyforge.org/misc/ruby-debugging.html#gui
    >>
    >> m.
    >>

    > Does NetBeans run with the standard Ruby, or only with jRuby?


    Yes. Actually until jruby-debug (Fast Debugger for JRuby) is available
    (everybody is really welcomed to join) the debugging with Ruby +
    ruby-debug-ide (fast debugger for Ruby) is highly preferred. The IDE
    will guide you through automatic settings and ruby-debug-ide gem
    installation and even tries to force you to use 'fast debugger' when it
    is appropriate ;)

    m.
     
    Martin Krauskopf, Jun 25, 2007
    #19
  20. Re: Ruby Debugger

    1. gem install ruby-debug

    2. Add the following to wherever you need a breakpoint:
    require 'ruby_debug'; debugger

    3. Debug away

    This is much faster and more usable than standard debug.

    --
    Alex
     
    Alexey Verkhovsky, Jun 25, 2007
    #20
    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. Alf P. Steinbach
    Replies:
    0
    Views:
    436
    Alf P. Steinbach
    Aug 18, 2003
  2. John Harrison
    Replies:
    4
    Views:
    6,927
    Default User
    Aug 19, 2003
  3. Icosahedron
    Replies:
    8
    Views:
    656
    Vivek
    Aug 21, 2003
  4. kevin
    Replies:
    1
    Views:
    373
    Kairi Zikpin
    Jul 21, 2006
  5. Wirianto Djunaidi
    Replies:
    2
    Views:
    203
    Wirianto Djunaidi
    Apr 29, 2008
Loading...

Share This Page