Helper to create multi-dimensional arrays

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)
# => [[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) {""}
# => [["", "", "", "", ""], ["", "", "", "", ""], ["", "", "", "", ""], ["", "", "", "", ""], ["", "", "", "", ""]]

···

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

I'm far too lazy.

I'd just do

a = [[0]*5]]*5

···

On 6/24/07, Anthony Martinez <pi@pihost.us> 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) {""}
# => [["", "", "", "", ""], ["", "", "", "", ""], ["", "", "", "", ""], ["", "", "", "", ""], ["", "", "", "", ""]]

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

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

Arg. don't do that. it makes the same array for each. :-/

···

On 6/24/07, Gregory Brown <gregory.t.brown@gmail.com> wrote:

On 6/24/07, Anthony Martinez <pi@pihost.us> 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

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:ds@iae.nl]
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

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

Jason

···

-----Original Message-----
From: Dick Summerfield [mailto:ds@iae.nl]
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

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?

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

···

On Mon, Jun 25, 2007 at 08:06:44AM +0900, Daniel DeLorme wrote:

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

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

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

This is great. Finally a sensible way to create multi-dimensional
arrays in Ruby. Thank you Anthony and Daniel.

Aditya

···

On Jun 24, 6:06 pm, Daniel DeLorme <dan...@dan42.com> wrote:

Anthony Martinez wrote:
> I came up with this method really quick to create x*y arrays in Ruby. It
> mimicksArray#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

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:ds@iae.nl]
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

I posted again saying you shouldn't use the approach I mentioned
because it creates copies of the same array.

···

On 6/25/07, Lloyd Linklater <lloyd@2live4.com> 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]]

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

--Colin

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.

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 :wink:

http://debug-commons.rubyforge.org/misc/ruby-debugging.html#gui

  m.

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

david

···

On Sunday 24 June 2007 16:31:00 Colin Summers wrote:

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

--Colin

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

--Colin

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 :wink:

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 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 :wink:

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

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 :wink:

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 :wink:

  m.