# Comparison question

**URL:** <https://www.ruby-forum.com/t/comparison-question/117340>\
**Category:** Ruby\
**Created:** [October 16, 2007, 2:45am UTC](https://www.ruby-forum.com/t/comparison-question/117340 "2007-10-16T02:45:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wpvanpaassen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/6f9a4e/32.png) [@wpvanpaassen](https://www.ruby-forum.com/u/wpvanpaassen)\
**Post date:** [October 16, 2007, 2:45am UTC](https://www.ruby-forum.com/t/comparison-question/117340/1 "2007-10-16T02:45:56Z")

</div>

I’m looking at the Comparable module in a book, and I can’t figure out  
how to do a simple comparison. What I want to do is a loop based on a  
counter. If the counter is less than 3, I want to execute the loop,  
otherwise I want the loop to end. I’m used to coding something like if  
$cnt \< 3 but in Ruby that doesn’t seem to work. Obviously I’m new to OO  
programming, so can someone give me a simple example of how to do this,  
or point me to a simple tutorial on how to do comparisons?  
Thanks

---

<div class="post-metadata">

**Author:** ![wpvanpaassen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/6f9a4e/32.png) [@wpvanpaassen](https://www.ruby-forum.com/u/wpvanpaassen)\
**Post date:** [October 16, 2007, 2:54am UTC](https://www.ruby-forum.com/t/comparison-question/117340/2 "2007-10-16T02:54:03Z")

</div>

Never mind, I figured it out, sorry!

Peter V. wrote:

> I’m looking at the Comparable module in a book, and I can’t figure out  
> how to do a simple comparison. What I want to do is a loop based on a  
> counter. If the counter is less than 3, I want to execute the loop,  
> otherwise I want the loop to end. I’m used to coding something like if  
> $cnt \< 3 but in Ruby that doesn’t seem to work. Obviously I’m new to OO  
> programming, so can someone give me a simple example of how to do this,  
> or point me to a simple tutorial on how to do comparisons?  
> Thanks

---

<div class="post-metadata">

**Author:** ![wpvanpaassen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/6f9a4e/32.png) [@wpvanpaassen](https://www.ruby-forum.com/u/wpvanpaassen)\
**Post date:** [October 16, 2007, 5:56am UTC](https://www.ruby-forum.com/t/comparison-question/117340/3 "2007-10-16T05:56:01Z")

</div>

Peter V. wrote:

> Never mind, I figured it out, sorry!

Here are some ways you might not have thought of:

a)  
arr = [“hello”, “world”, “goodbye”, “mars”]

arr.each\_with\_index do |elmt, i|  
if i == 3  
break  
end

puts elmt

end

–output:–  
hello  
world  
goodbye

b)  
(0…2).each {|i| print i}

–output:–  
012

c)  
3.times {|i| puts i}

–output:–  
0  
1  
2

---

<div class="post-metadata">

**Author:** ![wpvanpaassen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/6f9a4e/32.png) [@wpvanpaassen](https://www.ruby-forum.com/u/wpvanpaassen)\
**Post date:** [October 16, 2007, 11:38am UTC](https://www.ruby-forum.com/t/comparison-question/117340/4 "2007-10-16T11:38:25Z")

</div>

2007/10/16, Peter V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I’m looking at the Comparable module in a book, and I can’t figure out  
> how to do a simple comparison. What I want to do is a loop based on a  
> counter. If the counter is less than 3, I want to execute the loop,  
> otherwise I want the loop to end. I’m used to coding something like if  
> $cnt \< 3 but in Ruby that doesn’t seem to work. Obviously I’m new to OO  
> programming, so can someone give me a simple example of how to do this,  
> or point me to a simple tutorial on how to do comparisons?

[http://www.google.com/search?q=ruby+tutorial](http://www.google.com/search?q=ruby+tutorial)

> **[Ruby-Doc.org: Documenting the Ruby Language](https://ruby-doc.org/)**
>
> Fast, searchable Ruby documentation for core and standard libraries. Plus, links to tutorials, guides, books, and related sites.

robert
