tags:

views:

310

answers:

3

When I find the keyword "in" in ruby first time. I think maybe I can do that: 1 in (0..10) But it look like I cannot use it like that way.

Then I search it in ruby-lang.org, and google it. There is no answer!

What's the meaning of keyword "in" in ruby?

A: 

according to the pragmatic programmers book you se it as following

while *name*[, *name*]... in *expression* [do | :]
  body
end

so you use it in loops, sorry if this is vague, but i have only started learning ruby.

Craig
+1  A: 

You should be able to do the following:

for i in 0..10 do
  puts i
end

The expression 1 in (0..10) that you mention won't work because a constant (1) can't vary over a range - it's a constant! You need to name a variable before the in keyword.

Hope that helps.

See this page as well.

Mark Pim
Thank you!Because I come from F# and lisp. I always use "each" "map". I even forget the "for" loop keyword.
colder
You can also use "for in"-loops in F#
Mouk
Object#each is probably more Ruby-ish than for...in
Mike Woodhouse
To achieve the same result from 'for i in 0..10', one could use Fixnum#times i.e. '10.times do |i| ... end'. I think this is indeed more expressive and more "Ruby-ish".
Igor
A: 

In is also used inside of a conditions statement "pure sql"

Thing.find(:all, :conditions => ['id IN ?', somearray])