ruby:关键字“in”的含义是什么

时间:2021-11-27 03:22:57

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.

当我第一次在ruby中找到关键字“in”时。我想也许我可以这样做:1英寸(0..10)但看起来我不能像那样使用它。

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

然后我在ruby-lang.org中搜索它,然后google它。没有答案!

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

红宝石中关键词“in”的含义是什么?

2 个解决方案

#1


6  

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.

你提到的(0..10)中的表达式1将不起作用,因为常量(1)不能在一个范围内变化 - 它是一个常数!您需要在in关键字之前命名变量。

Hope that helps.

希望有所帮助。

See this page as well.

也请参阅此页面。

#2


0  

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.

所以你在循环中使用它,对不起,如果这是模糊的,但我只是开始学习ruby。

#1


6  

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.

你提到的(0..10)中的表达式1将不起作用,因为常量(1)不能在一个范围内变化 - 它是一个常数!您需要在in关键字之前命名变量。

Hope that helps.

希望有所帮助。

See this page as well.

也请参阅此页面。

#2


0  

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.

所以你在循环中使用它,对不起,如果这是模糊的,但我只是开始学习ruby。