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) {""}

=> [["", “”, “”, “”, “”], ["", “”, “”, “”, “”], ["", “”, “”, “”, “”],

["", “”, “”, “”, “”], ["", “”, “”, “”, “”]]

On 6/24/07, Anthony M. [email protected] 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

On 6/24/07, Gregory B. [email protected] wrote:

I’d just do

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

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

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 P.'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 F., Rich Kilmer, Jim W. 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.

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

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

Jason

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.

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

–Colin

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

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

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

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

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

–Colin

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

On Sunday 24 June 2007 16:31:00 Colin S. 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

Gregory B. 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]]

Martin K. wrote:

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

M. Edward (Ed) Borasky wrote:

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

On 6/25/07, Lloyd L. [email protected] wrote:

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.

M. Edward (Ed) Borasky wrote:

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.

On Jun 24, 6:06 pm, Daniel DeLorme [email protected] wrote:

   Array.new(n, &block)

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

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

Aditya

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