Ruby将命令放入返回nil的模块中
我正在通过LearnTocodethehardway.com工作 在ex25上。在这个示例中,有一个模块,它有一系列返回或打印值的方法。Print\u last\u word方法在提供字符串数组时仅将nil置为空。即使在他的示例输出中,它也会这样做。我的问题是为什么 准确地说,它不把Ruby将命令放入返回nil的模块中,ruby,Ruby,我正在通过LearnTocodethehardway.com工作 在ex25上。在这个示例中,有一个模块,它有一系列返回或打印值的方法。Print\u last\u word方法在提供字符串数组时仅将nil置为空。即使在他的示例输出中,它也会这样做。我的问题是为什么 准确地说,它不把nil——它把最后一个字放进去,然后返回nil。以下是示例输出: >> Ex25.print_last_word(words) wait. # <- this is the output =&
nil
——它把最后一个字放进去,然后返回nil
。以下是示例输出:
>> Ex25.print_last_word(words)
wait. # <- this is the output
=> nil # <- this is the return value
这是因为[“foo”、“bar”、“baz”].pop(0)
返回一个空数组,put[]
只返回nil
,而不打印任何内容
工作实现(以练习的方式)可能如下所示:
module Ex25
def Ex25.print_first_word(words)
word = words.shift
puts word
end
end
准确地说,它不把nil
——它把最后一个字放进去,然后返回nil
。以下是示例输出:
>> Ex25.print_last_word(words)
wait. # <- this is the output
=> nil # <- this is the return value
这是因为[“foo”、“bar”、“baz”].pop(0)
返回一个空数组,put[]
只返回nil
,而不打印任何内容
工作实现(以练习的方式)可能如下所示:
module Ex25
def Ex25.print_first_word(words)
word = words.shift
puts word
end
end
准确地说,它不把nil
——它把最后一个字放进去,然后返回nil
。以下是示例输出:
>> Ex25.print_last_word(words)
wait. # <- this is the output
=> nil # <- this is the return value
这是因为[“foo”、“bar”、“baz”].pop(0)
返回一个空数组,put[]
只返回nil
,而不打印任何内容
工作实现(以练习的方式)可能如下所示:
module Ex25
def Ex25.print_first_word(words)
word = words.shift
puts word
end
end
准确地说,它不把nil
——它把最后一个字放进去,然后返回nil
。以下是示例输出:
>> Ex25.print_last_word(words)
wait. # <- this is the output
=> nil # <- this is the return value
这是因为[“foo”、“bar”、“baz”].pop(0)
返回一个空数组,put[]
只返回nil
,而不打印任何内容
工作实现(以练习的方式)可能如下所示:
module Ex25
def Ex25.print_first_word(words)
word = words.shift
puts word
end
end
为使其更易于理解,请执行以下操作: “puts”从不用于其返回值。它的副作用是,如果你想要一个返回一个单词的方法,你可以用这个单词做一些事情
words = ["apple", "cucumber", "apple"]
def give_me_first_word(array_of_words)
array_of_words.first
end
variable_to_be_used_later = give_me_first_word(words)
此函数将返回“apple”(在本例中,变量将被指定为“apple”),但它不会将任何内容放到屏幕上。然后,该值将在另一个程序或函数中使用
如果您输入了一个值,那么就不需要将它返回给任何其他程序,因为它已经达到了它的目的。这实际上是直观的,因为如果看跌期权也返回了值,它将做两件事。与“puts”相对应的是“return”,它只返回值,不显示在屏幕上。为了更容易理解: “puts”从不用于其返回值。它的副作用是,如果你想要一个返回一个单词的方法,你可以用这个单词做一些事情
words = ["apple", "cucumber", "apple"]
def give_me_first_word(array_of_words)
array_of_words.first
end
variable_to_be_used_later = give_me_first_word(words)
此函数将返回“apple”(在本例中,变量将被指定为“apple”),但它不会将任何内容放到屏幕上。然后,该值将在另一个程序或函数中使用
如果您输入了一个值,那么就不需要将它返回给任何其他程序,因为它已经达到了它的目的。这实际上是直观的,因为如果看跌期权也返回了值,它将做两件事。与“puts”相对应的是“return”,它只返回值,不显示在屏幕上。为了更容易理解: “puts”从不用于其返回值。它的副作用是,如果你想要一个返回一个单词的方法,你可以用这个单词做一些事情
words = ["apple", "cucumber", "apple"]
def give_me_first_word(array_of_words)
array_of_words.first
end
variable_to_be_used_later = give_me_first_word(words)
此函数将返回“apple”(在本例中,变量将被指定为“apple”),但它不会将任何内容放到屏幕上。然后,该值将在另一个程序或函数中使用
如果您输入了一个值,那么就不需要将它返回给任何其他程序,因为它已经达到了它的目的。这实际上是直观的,因为如果看跌期权也返回了值,它将做两件事。与“puts”相对应的是“return”,它只返回值,不显示在屏幕上。为了更容易理解: “puts”从不用于其返回值。它的副作用是,如果你想要一个返回一个单词的方法,你可以用这个单词做一些事情
words = ["apple", "cucumber", "apple"]
def give_me_first_word(array_of_words)
array_of_words.first
end
variable_to_be_used_later = give_me_first_word(words)
此函数将返回“apple”(在本例中,变量将被指定为“apple”),但它不会将任何内容放到屏幕上。然后,该值将在另一个程序或函数中使用
如果您输入了一个值,那么就不需要将它返回给任何其他程序,因为它已经达到了它的目的。这实际上是直观的,因为如果看跌期权也返回了值,它将做两件事。与“puts”相对应的是“return”,它只返回值,不显示在屏幕上。puts总是返回nil。这就是它所做的,sputs总是返回nil。这就是它所做的,sputs总是返回nil。这就是它所做的,sputs总是返回nil。这正是它所做的。我知道一个方法总是返回最后一行,但是两个输出不是通过函数的最后两行返回的第一个字和最后一个字
def Ex25.print_first_和_last(句子)words=Ex25.break_words(句子)Ex25.print_first_words(words)Ex25.print_last_words(words)end
@ChrisCPO在print_first_word
中似乎有一个bug(不是print_last_word
)-我已经更新了我的答案我知道一个方法总是返回最后一行,但是通过函数的最后两行,两个输出不是第一个字和最后一个字def Ex25.print\u first\u and\u last(句子)words=Ex25.break\u words(句子)Ex25.print\u first\u words(单词)Ex25.print\u last\u words(单词)end
@ChrisCPO在print\u first\u word
中似乎有一个bug(不是print\u last\u word
)-我已经更新了我的答案我知道一种方法