Ruby if else 语句
Ruby if else statement
我被要求:
Define a method called first_longer_than_second
with a parameter called first
and another called second
. The method will return true
if the first word passed in is greater than or equal to the length of the second word. It returns false
otherwise.
这是我想出的代码:
def first_longer_than_second(first, second)
if first >= second
return true;
else
return false;
end
end
我正在拨打的电话:
first_longer_than_second('pneumonoultramicroscopicsilicovolcanoconiosis', 'k') #=> false
first_longer_than_second('apple', 'prune') #=> true
出于某种原因,在 repl.it 上我只得到输出 false
我在平台上收到此错误消息我实际上是要在以下平台上完成此任务:
expected #<TrueClass:20> => true
got #<FalseClass:0> => false
Compared using equal?, which compares object identity,
but expected and actual are not the same object. Use
`expect(actual).to eq(expected)` if you don't care about
object identity in this example.
exercise_spec.rb:42:in `block (2 levels) in <top (required)>'
尝试了很多东西,但烦人地坚持了一些看起来应该很简单的东西......
Define a method called first_longer_than_second
with a parameter called first
and another called second
. The method will return true
if the first
word passed in is greater than or equal to the length of the second
word. It returns false
otherwise.
您的代码:
def first_longer_than_second(first, second)
if first >= second
return true;
else
return false;
end
end
首先,您的代码不符合要求。他们要求比较两个参数的长度。 if
条件应该是:
if first.length >= second.length
请参阅 String#length
的文档。
关于Ruby的语法,语句后不需要分号(;
)。与 Javascript 一样,Ruby 语句可以使用分号和换行符终止。分号用于分隔同一行上的两个语句。
接下来,与Javascript(以及许多其他语言)一样,您可以直接return比较的结果(而不是将其放入if
语句中returns true
/false
):
def first_longer_than_second(first, second)
return first.length >= second.length
end
最后的改进使其看起来像 Ruby(而不是 Javascript 或 PHP):一个 Ruby 函数 returns 的值它计算的最后一个表达式;这使得 return
关键字在这里变得多余。
您的代码应该是:
def first_longer_than_second(first, second)
first.length >= second.length
end
我被要求:
Define a method called
first_longer_than_second
with a parameter calledfirst
and another calledsecond
. The method willreturn true
if the first word passed in is greater than or equal to the length of the second word. Itreturns false
otherwise.
这是我想出的代码:
def first_longer_than_second(first, second)
if first >= second
return true;
else
return false;
end
end
我正在拨打的电话:
first_longer_than_second('pneumonoultramicroscopicsilicovolcanoconiosis', 'k') #=> false
first_longer_than_second('apple', 'prune') #=> true
出于某种原因,在 repl.it 上我只得到输出 false
我在平台上收到此错误消息我实际上是要在以下平台上完成此任务:
expected #<TrueClass:20> => true
got #<FalseClass:0> => false
Compared using equal?, which compares object identity,
but expected and actual are not the same object. Use
`expect(actual).to eq(expected)` if you don't care about
object identity in this example.
exercise_spec.rb:42:in `block (2 levels) in <top (required)>'
尝试了很多东西,但烦人地坚持了一些看起来应该很简单的东西......
Define a method called
first_longer_than_second
with a parameter calledfirst
and another calledsecond
. The method will returntrue
if thefirst
word passed in is greater than or equal to the length of thesecond
word. It returnsfalse
otherwise.
您的代码:
def first_longer_than_second(first, second)
if first >= second
return true;
else
return false;
end
end
首先,您的代码不符合要求。他们要求比较两个参数的长度。 if
条件应该是:
if first.length >= second.length
请参阅 String#length
的文档。
关于Ruby的语法,语句后不需要分号(;
)。与 Javascript 一样,Ruby 语句可以使用分号和换行符终止。分号用于分隔同一行上的两个语句。
接下来,与Javascript(以及许多其他语言)一样,您可以直接return比较的结果(而不是将其放入if
语句中returns true
/false
):
def first_longer_than_second(first, second)
return first.length >= second.length
end
最后的改进使其看起来像 Ruby(而不是 Javascript 或 PHP):一个 Ruby 函数 returns 的值它计算的最后一个表达式;这使得 return
关键字在这里变得多余。
您的代码应该是:
def first_longer_than_second(first, second)
first.length >= second.length
end