Hacking the Ruby interpreter

T

Tim Hunter

I've got an idea for a hack^H^H^H^H enhancement to the interpreter that I'd
like to try out. If it works, I was thinking of distributing it as a patch.
If somebody wanted to use it, they'd have to d/l the Ruby tarball, apply
the patch, build, and install.

Does this sound like a reasonable approach? Any catches I'm overlooking? Of
course the license would be the same as Ruby's.

What version of Ruby should I use? I suppose the stable 1.8.1, unless of
course 1.8.2 becomes production before I'm done, which is possible.

My plan is to simply check out the v1_8_1 sources from CVS, make the
changes, and make a patch with cvs diff. Is this the right approach?
 
P

Phil Tomson

I've got an idea for a hack^H^H^H^H enhancement to the interpreter that I'd
like to try out. If it works, I was thinking of distributing it as a patch.
If somebody wanted to use it, they'd have to d/l the Ruby tarball, apply
the patch, build, and install.

Does this sound like a reasonable approach? Any catches I'm overlooking? Of
course the license would be the same as Ruby's.

What version of Ruby should I use? I suppose the stable 1.8.1, unless of
course 1.8.2 becomes production before I'm done, which is possible.

My plan is to simply check out the v1_8_1 sources from CVS, make the
changes, and make a patch with cvs diff. Is this the right approach?

What enhancements are you planning to add? There are a couple of problems
with things like this:

1) Not many people are going to apply the patch. It's a lot more work
than installing a package and it could make your version of Ruby
incompatible with others.
2) You could find that your patch needs to be adjusted everytime a new
version of Ruby comes out. It'll probably be version-specific which again
will limit it's adoption.

If you're trying to do a proof-of-concept with your patch, then perhaps
you should submit an RCR and include your patch with it. Then it might
have a chance at making it into the standard Ruby distirbution.

Phil
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Hacking the Ruby interpreter"

|I've got an idea for a hack^H^H^H^H enhancement to the interpreter that I'd
|like to try out. If it works, I was thinking of distributing it as a patch.
|If somebody wanted to use it, they'd have to d/l the Ruby tarball, apply
|the patch, build, and install.

If the idea is a really good one, send us to merge in.

|Does this sound like a reasonable approach? Any catches I'm overlooking? Of
|course the license would be the same as Ruby's.
|
|What version of Ruby should I use? I suppose the stable 1.8.1, unless of
|course 1.8.2 becomes production before I'm done, which is possible.

CVS HEAD (1.9.0) is a reasonable test bed for new ideas.

matz.
 
M

Mauricio Fernández

I've got an idea for a hack^H^H^H^H enhancement to the interpreter that I'd
like to try out. If it works, I was thinking of distributing it as a patch.

It might be possible to implement it in pure Ruby (look at evil.rb).
If somebody wanted to use it, they'd have to d/l the Ruby tarball, apply
the patch, build, and install.

Does this sound like a reasonable approach? Any catches I'm overlooking? Of
course the license would be the same as Ruby's.

What version of Ruby should I use? I suppose the stable 1.8.1, unless of
course 1.8.2 becomes production before I'm done, which is possible.

Better use the CVS stable (ruby_1_8) branch.
My plan is to simply check out the v1_8_1 sources from CVS, make the
changes, and make a patch with cvs diff. Is this the right approach?

Should work.
But don't expect many people to apply your patch... :-|
 
P

Paul Brannan

I've got an idea for a hack^H^H^H^H enhancement to the interpreter that I'd
like to try out. If it works, I was thinking of distributing it as a patch.
If somebody wanted to use it, they'd have to d/l the Ruby tarball, apply
the patch, build, and install.

If at all possible, implement it as an extension first so people who
want to try it can do so without patching Ruby. Sometimes this is not
possible; other times it is possible but requires weird hacks.

Paul
 
M

Markus

I've got an idea for a hack^H^H^H^H enhancement to the interpreter that I'd
like to try out.

I can't believe no one has asked you the obvious question yet. I
feel like the impetuous kid in the company of saintly elders. But here
goes:

So, what's it do? Is it cool? Huh, huh? Is it a secret?
Will you tell us what the idea was even if it doesn't work?
Will you share if it does?

Eh hem. We return to our previously scheduled adulthood, already in
progress.

-- MarkusQ (collaborating with my inner child)
 
B

Bill Guindon

I can't believe no one has asked you the obvious question yet. I
feel like the impetuous kid in the company of saintly elders. But here
goes:

So, what's it do? Is it cool? Huh, huh? Is it a secret?
Will you tell us what the idea was even if it doesn't work?
Will you share if it does?

Eh hem. We return to our previously scheduled adulthood, already in
progress.

-- MarkusQ (collaborating with my inner child)

my inner child says thanks ;)
 
B

Barry Sperling

Reading the recent posts about ant-wars, I thought that, as a nuby, it
would be interesting to try to recreate the ant-wars engine. I looked
at their code a little, and then started in. I appreciate that James
Edward Gray II provided us with his code, but I'll look at it after I've
made more progress.
I decided to create 2-d arrays to hold the data for given positions (
hexagonal blocks ). One would hold the amount of food at a position,
another would hold the traversability of it, etc. My first attempt
failed in a strange way and the point of this email is: why?

Method 1

cols = 10
empty_row_int = Array.new( cols, 0 )
$food_layout = Array.new
rows.times { $food_layout.push( empty_row_int ) }
puts $food_layout[ 0 ][ 0 ] # 0
$food_layout[ 0 ][ 0 ] = 6 # SET FOOD ITEMS IN HEXAGON
puts $food_layout[ 0 ][ 0 ] # 6
puts $food_layout[ 1 ][ 0 ] # 6 WRONG!

Then I read some archived emails from the group and found Matrix
mentioned, but a matrix couldn't be changed by assignment. However, it
COULD be converted into an array, and so we get:

Method 2

require 'matrix'
maf = Matrix.zero( 10 ) # A SQUARE MATRIX, 10X10, INITIALIZED TO 0
$food2_layout = maf.to_a # MATRIX TO ARRAY
puts $food2_layout[ 0 ][ 0 ] # 0
$food2_layout[ 0 ][ 0 ] = 6 # SET FOOD ITEMS IN HEX
puts $food2_layout[ 0 ][ 0 ] # 6
puts $food2_layout[ 1 ][ 0 ] # 0

Then I found another method was mentioned as listed in The Ruby Way:

Method 3

matrix = ( 0..9 ).map { ( 0..9 ).map { 0 } }

and when I use puts with the code as above, it works.
I understand Method 2, I "sort of" understand Method 3, but I don't know
why "my" Method 1 doesn't work.
Thanks,
Barry
 
J

James Edward Gray II

Reading the recent posts about ant-wars, I thought that, as a nuby,
it would be interesting to try to recreate the ant-wars engine. I
looked at their code a little, and then started in. I appreciate that
James Edward Gray II provided us with his code, but I'll look at it
after I've made more progress.

You'll enjoy it, it's a fun project.
I decided to create 2-d arrays to hold the data for given positions (
hexagonal blocks ). One would hold the amount of food at a position,
another would hold the traversability of it, etc. My first attempt
failed in a strange way and the point of this email is: why?

See inline comments below...
Method 1

cols = 10
empty_row_int = Array.new( cols, 0 )

Here you create an Array object and store a reference to that object in
empty_row_int.
$food_layout = Array.new
rows.times { $food_layout.push( empty_row_int ) }

And here, you set each row to that same reference you created above.
You aren't making a bunch of unique rows, you're reusing the same row
over and over again. Change one and you change them all.

To fix it, try creating a new array as you define each row. Remember,
Ruby variables just hold references to the actual objects, not the
objects themselves.

Hope that helps.

James Edward Gray II
puts $food_layout[ 0 ][ 0 ] # 0
$food_layout[ 0 ][ 0 ] = 6 # SET FOOD ITEMS IN HEXAGON
puts $food_layout[ 0 ][ 0 ] # 6
puts $food_layout[ 1 ][ 0 ] # 6 WRONG!
 
M

Markus

I suspect method 1 doesn't do what you expect because you create one
single row and then make an array in which every element is that same
row. Try instead:

Method 1'

cols = 10
$food_layout = Array.new
rows.times { $food_layout.push( Array.new( cols, 0 ) ) }
puts $food_layout[ 0 ][ 0 ] # 0
$food_layout[ 0 ][ 0 ] = 6 # SET FOOD ITEMS IN HEXAGON
puts $food_layout[ 0 ][ 0 ] # 6
puts $food_layout[ 1 ][ 0 ] # 0

-- MarkusQ
 
M

mark sparshatt

Barry said:
Reading the recent posts about ant-wars, I thought that, as a
nuby, it would be interesting to try to recreate the ant-wars engine.
I looked at their code a little, and then started in. I appreciate
that James Edward Gray II provided us with his code, but I'll look at
it after I've made more progress.
I decided to create 2-d arrays to hold the data for given
positions ( hexagonal blocks ). One would hold the amount of food at
a position, another would hold the traversability of it, etc. My
first attempt failed in a strange way and the point of this email is:
why?

Method 1

cols = 10
empty_row_int = Array.new( cols, 0 )

this creates an array
$food_layout = Array.new
rows.times { $food_layout.push( empty_row_int ) }

this pushes that array into $food_layout rows times.
puts $food_layout[ 0 ][ 0 ] # 0
$food_layout[ 0 ][ 0 ] = 6 # SET FOOD ITEMS IN HEXAGON
puts $food_layout[ 0 ][ 0 ] # 6
puts $food_layout[ 1 ][ 0 ] # 6 WRONG!
$food_layout[0] and $food_layout[1] both refer to the same array object
(empty_row_int) so they both hold the same values.

The correct way to do this would be

cols = 10
$food_layout = Array.new
rows.times { $food_layout.push( Array.new( cols, 0 ) ) }
puts $food_layout[ 0 ][ 0 ] # 0
$food_layout[ 0 ][ 0 ] = 6 # SET FOOD ITEMS IN HEXAGON

HTH
 
A

Austin Ziegler

Reading the recent posts about ant-wars, I thought that, as a nuby,
it would be interesting to try to recreate the ant-wars engine. I
looked at their code a little, and then started in. I appreciate
that James Edward Gray II provided us with his code, but I'll look
at it after I've made more progress.

I decided to create 2-d arrays to hold the data for given positions
(hexagonal blocks). One would hold the amount of food at a position,
another would hold the traversability of it, etc. My first attempt
failed in a strange way and the point of this email is: why?
Method 1
cols = 10
empty_row_int = Array.new( cols, 0 )
$food_layout = Array.new
rows.times { $food_layout.push( empty_row_int ) }
puts $food_layout[ 0 ][ 0 ] # 0
$food_layout[ 0 ][ 0 ] = 6 # SET FOOD ITEMS IN HEXAGON
puts $food_layout[ 0 ][ 0 ] # 6
puts $food_layout[ 1 ][ 0 ] # 6 WRONG!

Try:

cols = 10
rows = 10
food_layout = Array.new(rows) { Array.new(cols, 0) }

puts food_layout[0][0] # -> 0
food_layout[0][0] = 6 # Set food items in hexagon
puts food_layout[0][0] # -> 6
puts food_layout[1][0] # -> 0

-austin
 

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

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top