# Syntax translation Java - Ruby

**URL:** <https://www.ruby-forum.com/t/syntax-translation-java-ruby/176368>\
**Category:** Ruby\
**Created:** [September 29, 2009, 6:24pm UTC](https://www.ruby-forum.com/t/syntax-translation-java-ruby/176368 "2009-09-29T18:24:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mark42](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/65b543/32.png) [@mark42](https://www.ruby-forum.com/u/mark42)\
**Post date:** [September 29, 2009, 6:24pm UTC](https://www.ruby-forum.com/t/syntax-translation-java-ruby/176368/1 "2009-09-29T18:24:44Z")

</div>

have this code in Java:

boolean monkey = true;  
for(i=0; monkey; i++){  
if(index[i] == 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.

---

<div class="post-metadata">

**Author:** ![mark42](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/65b543/32.png) [@mark42](https://www.ruby-forum.com/u/mark42)\
**Post date:** [September 29, 2009, 7:11pm UTC](https://www.ruby-forum.com/t/syntax-translation-java-ruby/176368/2 "2009-09-29T19:11:07Z")

</div>

On Tue, Sep 29, 2009 at 5:24 PM, Jake Brightmatter  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

–  
Paul S.  
[http://www.nomadicfun.co.uk](http://www.nomadicfun.co.uk)

removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![mark42](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/65b543/32.png) [@mark42](https://www.ruby-forum.com/u/mark42)\
**Post date:** [September 30, 2009, 4:10am UTC](https://www.ruby-forum.com/t/syntax-translation-java-ruby/176368/3 "2009-09-30T04:10:59Z")

</div>

On Sep 29, 12:32 pm, Paul S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > for(i=0; monkey; i++){  
> > found parking spot. I really can’t figure out why I need to uniquely  
> > Paul S.[http://www.nomadicfun.co.uk](http://www.nomadicfun.co.uk)
> 
> removed\_email\_address@domain.invalid

An alternate way

while true do

# your code

# break if

end

removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![mark42](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/65b543/32.png) [@mark42](https://www.ruby-forum.com/u/mark42)\
**Post date:** [September 30, 2009, 5:02am UTC](https://www.ruby-forum.com/t/syntax-translation-java-ruby/176368/4 "2009-09-30T05:02:30Z")

</div>

On Sep 29, 2009, at 09:24 , Jake Brightmatter wrote:

> 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[i] = 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.

---

<div class="post-metadata">

**Author:** ![mark42](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/65b543/32.png) [@mark42](https://www.ruby-forum.com/u/mark42)\
**Post date:** [September 30, 2009, 5:41am UTC](https://www.ruby-forum.com/t/syntax-translation-java-ruby/176368/5 "2009-09-30T05:41:08Z")

</div>

On Tue, Sep 29, 2009 at 11:24 AM, Jake Brightmatter  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> I am aware that it makes no sense by itself so I will explain. I am

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 )

---

<div class="post-metadata">

**Author:** ![mark42](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/65b543/32.png) [@mark42](https://www.ruby-forum.com/u/mark42)\
**Post date:** [September 30, 2009, 2:21pm UTC](https://www.ruby-forum.com/t/syntax-translation-java-ruby/176368/6 "2009-09-30T14:21:34Z")

</div>

Ryan D. wrote:

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