Helper to create multi-dimensional arrays

A

Anthony Martinez

--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--
 
G

Gregory Brown

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
 
G

Gregory Brown

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. :-/
 
D

Dick Summerfield

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.
 
J

Jason Vinson

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:[email protected]]
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.
 
J

Jason Vinson

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

Jason

-----Original Message-----
From: Dick Summerfield [mailto:[email protected]]
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.
 
D

Dick Summerfield

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.

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:[email protected]]
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
 
D

Daniel DeLorme

Anthony said:
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
 
M

M. Edward (Ed) Borasky

Colin said:
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.
 
A

Anthony Martinez

--UlsYxwg8UDQn+EKZ
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

=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--
 
L

Lloyd Linklater

Gregory said:
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]]
 
G

Gregory Brown

Gregory said:
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.
 
C

Charles Oliver Nutter

M. Edward (Ed) Borasky said:
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

Martin Krauskopf

M. Edward (Ed) Borasky said:
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.
 
A

Alexey Verkhovsky

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top