syntax translation Java - Ruby

J

Jake Brightmatter

have this code in Java:

boolean monkey = true;
for(i=0; monkey; i++){
if(index == null) {
put.something.new(i);
break;
}
}

I am aware that it makes no sense by itself so I will explain. I am
iterating through a (Sequel) database nodes. I am looking for a node
I.D. that is not being used. Basically, I am looking for and open
parking spot. When I find one, I insert my new data using the newly
found parking spot. I really can't figure out why I need to uniquely
identify each node by an I.D. as it is already uniquely identified by
location in the tree but that is a different matter entirely. All I
need is how to write a nice infinite 'for' loop in Ruby.
 
P

Paul Smith

have this code in Java:

boolean monkey =3D true;
for(i=3D0; monkey; i++){
=A0if(index =3D=3D null) {
=A0 =A0put.something.new(i);
=A0 =A0break;
=A0}
}

I am aware that it makes no sense by itself so I will explain. =A0I am
iterating through a (Sequel) database nodes. =A0I am looking for a node
I.D. that is not being used. =A0Basically, I am looking for and open
parking spot. =A0When I find one, I insert my new data using the newly
found parking spot. I really can't figure out why I need to uniquely
identify each node by an I.D. as it is already uniquely identified by
location in the tree but that is a different matter entirely. =A0All I
need is how to write a nice infinite 'for' loop in Ruby.


loop {}



--=20
Paul Smith
http://www.nomadicfun.co.uk

(e-mail address removed)
 
B

bbiker

have this code in Java:
boolean monkey = true;
for(i=0; monkey; i++){
 if(index == null) {
   put.something.new(i);
   break;
 }
}

I am aware that it makes no sense by itself so I will explain.  I am
iterating through a (Sequel) database nodes.  I am looking for a node
I.D. that is not being used.  Basically, I am looking for and open
parking spot.  When I find one, I insert my new data using the newly
found parking spot. I really can't figure out why I need to uniquely
identify each node by an I.D. as it is already uniquely identified by
location in the tree but that is a different matter entirely.  All I
need is how to write a nice infinite 'for' loop in Ruby.

loop {}


An alternate way

while true do
# your code
# break if <condition>
end

(e-mail address removed)
 
R

Ryan Davis

have this code in Java:

boolean monkey = true;
for(i=0; monkey; i++){
if(index == null) {
put.something.new(i);
break;
}
}

I am aware that it makes no sense by itself so I will explain. I am
iterating through a (Sequel) database nodes. I am looking for a node
I.D. that is not being used. Basically, I am looking for and open
parking spot. When I find one, I insert my new data using the newly
found parking spot. I really can't figure out why I need to uniquely
identify each node by an I.D. as it is already uniquely identified by
location in the tree but that is a different matter entirely. All I
need is how to write a nice infinite 'for' loop in Ruby.


No, that's what you THINK you want because you're coming from java...

index is a bad name for a collection, so I'm going to call it
parking_spots.

parking_spots.each_with_index do |parking_spot, i|
next if parking_spot
parking_spots = my_car
break
end

but honestly if you're doing this against a sequel db, you're it
wrong. You should use the power of the DB instead of doing tons of
roundtrips in ruby.
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

have this code in Java:

boolean monkey = true;
for(i=0; monkey; i++){
if(index == null) {
put.something.new(i);
break;
}
}

I am aware that it makes no sense by itself so I will explain. I am
iterating through a (Sequel) database nodes. I am looking for a node
I.D. that is not being used. Basically, I am looking for and open
parking spot. When I find one, I insert my new data using the newly
found parking spot. I really can't figure out why I need to uniquely
identify each node by an I.D. as it is already uniquely identified by
location in the tree but that is a different matter entirely. All I
need is how to write a nice infinite 'for' loop in Ruby.

I know you are asking about the Ruby, but in your Java code, using the
variable monkey is rather confusing, and doesn't seem to have any purpose
except to hold the value true, to submit to the conditional portion of your
for loop. This isn't necessary, as you can directly place true into that
location:

for( i=0 ; true ; ++i )
 
C

Clifford Heath

Ryan said:
but honestly if you're doing this against a sequel db, you're it
wrong. You should use the power of the DB instead of doing tons of
roundtrips in ruby.

Right - what you want is:

SELECT id+1 FROM table AS t WHERE NOT EXISTS(SELECT * FROM table AS n WHERE t.id+1 = n.id) LIMIT 1

that is, find (just the first) successor of the first id that doesn't have a successor.

Otherwise just "SELECT max(id)+1 from table" if you don't need to fill vacated spots.
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top