Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在RubyonRails中传递参数_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 如何在RubyonRails中传递参数

Ruby on rails 如何在RubyonRails中传递参数,ruby-on-rails,ruby,Ruby On Rails,Ruby,我对下面的代码有一个问题,它不需要参数,我想创建一个方法来返回答案。我不知道如何传递值。 请问这个概念的名字是什么,这样我就可以读出来了 describe "reverser" do it "reverses the string returned by the default block" do result = reverser do "hello" end result.should == "olleh" end end def reverser(sent) word

我对下面的代码有一个问题,它不需要参数,我想创建一个方法来返回答案。我不知道如何传递值。 请问这个概念的名字是什么,这样我就可以读出来了

describe "reverser" do
it "reverses the string returned by the default block" do
  result = reverser do
    "hello"
  end
  result.should == "olleh"
end
end

def reverser(sent)
words = sent.split(" ")
  i = 0
  while i < words.length
   words[i] = words[i].reverse
   i += 1
  end
  words.join(" ")
end
描述“反向器”的作用
它“反转默认块返回的字符串”do
结果=反向器do
“你好”
结束
result.should==“olleh”
结束
结束
def反向器(已发送)
words=sent.split(“”)
i=0
而我则是
您的反向器获取一个块,您可以调用该块来获取其返回值:

def reverser(&block)
  string = block.call
  string.reverse
end

注意:您不需要
字符串
变量。我只是出于可读性的原因使用它。您也可以只编写
block.call.reverse

您的反向器获取您调用的块以获取其返回值:

def reverser(&block)
  string = block.call
  string.reverse
end
def reverser
  yield.reverse
end

注意:您不需要
字符串
变量。我只是出于可读性的原因使用它。您也可以只编写
block.call.reverse

您的反向器获取您调用的块以获取其返回值:

def reverser(&block)
  string = block.call
  string.reverse
end
def reverser
  yield.reverse
end

注意:您不需要
字符串
变量。我只是出于可读性的原因使用它。您也可以只编写
block.call.reverse

您的反向器获取您调用的块以获取其返回值:

def reverser(&block)
  string = block.call
  string.reverse
end
def reverser
  yield.reverse
end
注意:您不需要
字符串
变量。我只是出于可读性的原因使用它。您也可以只编写
block.call.reverse

def reverser
  yield.reverse
end
以下是相关代码:

result = reverser do
  "hello"
end
这相当于:

result = reverser() do
  "hello"
end
分解如下:

    method call -+      +- 'do' marks the start of the block
                 |      |
                 V      V 
    result = reverser() do
      "hello"
    end
     ^
     |
     +--- end of the block
一个块就像一个方法,就好像这个块是作为一个不可见的参数传递给reverse()方法一样。在reverse()内部,可以使用
yield
调用块:

def reverser
  return_value_of_block = yield
  return_value_of_block.reverse
end
编写
yield
,或
yield()
,就像编写
未修改的块方法()。换句话说,
yield
是ruby给block方法起的名字

如果该块应该接受参数,那么该块将如下所示:

result = do_stuff do |x|
  x * 2
end
def do_stuff
  return_value_of_block = yield(4)  #call block with the argument 4
end
do_stuff()将如下所示:

result = do_stuff do |x|
  x * 2
end
def do_stuff
  return_value_of_block = yield(4)  #call block with the argument 4
end
请问这个概念的名字是什么,这样我就可以读出来了

describe "reverser" do
it "reverses the string returned by the default block" do
  result = reverser do
    "hello"
  end
  result.should == "olleh"
end
end

def reverser(sent)
words = sent.split(" ")
  i = 0
  while i < words.length
   words[i] = words[i].reverse
   i += 1
  end
  words.join(" ")
end
搜索
ruby块

以下是相关代码:

result = reverser do
  "hello"
end
这相当于:

result = reverser() do
  "hello"
end
分解如下:

    method call -+      +- 'do' marks the start of the block
                 |      |
                 V      V 
    result = reverser() do
      "hello"
    end
     ^
     |
     +--- end of the block
一个块就像一个方法,就好像这个块是作为一个不可见的参数传递给reverse()方法一样。在reverse()内部,可以使用
yield
调用块:

def reverser
  return_value_of_block = yield
  return_value_of_block.reverse
end
编写
yield
,或
yield()
,就像编写
未修改的块方法()。换句话说,
yield
是ruby给block方法起的名字

如果该块应该接受参数,那么该块将如下所示:

result = do_stuff do |x|
  x * 2
end
def do_stuff
  return_value_of_block = yield(4)  #call block with the argument 4
end
do_stuff()将如下所示:

result = do_stuff do |x|
  x * 2
end
def do_stuff
  return_value_of_block = yield(4)  #call block with the argument 4
end
请问这个概念的名字是什么,这样我就可以读出来了

describe "reverser" do
it "reverses the string returned by the default block" do
  result = reverser do
    "hello"
  end
  result.should == "olleh"
end
end

def reverser(sent)
words = sent.split(" ")
  i = 0
  while i < words.length
   words[i] = words[i].reverse
   i += 1
  end
  words.join(" ")
end
搜索
ruby块

以下是相关代码:

result = reverser do
  "hello"
end
这相当于:

result = reverser() do
  "hello"
end
分解如下:

    method call -+      +- 'do' marks the start of the block
                 |      |
                 V      V 
    result = reverser() do
      "hello"
    end
     ^
     |
     +--- end of the block
一个块就像一个方法,就好像这个块是作为一个不可见的参数传递给reverse()方法一样。在reverse()内部,可以使用
yield
调用块:

def reverser
  return_value_of_block = yield
  return_value_of_block.reverse
end
编写
yield
,或
yield()
,就像编写
未修改的块方法()。换句话说,
yield
是ruby给block方法起的名字

如果该块应该接受参数,那么该块将如下所示:

result = do_stuff do |x|
  x * 2
end
def do_stuff
  return_value_of_block = yield(4)  #call block with the argument 4
end
do_stuff()将如下所示:

result = do_stuff do |x|
  x * 2
end
def do_stuff
  return_value_of_block = yield(4)  #call block with the argument 4
end
请问这个概念的名字是什么,这样我就可以读出来了

describe "reverser" do
it "reverses the string returned by the default block" do
  result = reverser do
    "hello"
  end
  result.should == "olleh"
end
end

def reverser(sent)
words = sent.split(" ")
  i = 0
  while i < words.length
   words[i] = words[i].reverse
   i += 1
  end
  words.join(" ")
end
搜索
ruby块

以下是相关代码:

result = reverser do
  "hello"
end
这相当于:

result = reverser() do
  "hello"
end
分解如下:

    method call -+      +- 'do' marks the start of the block
                 |      |
                 V      V 
    result = reverser() do
      "hello"
    end
     ^
     |
     +--- end of the block
一个块就像一个方法,就好像这个块是作为一个不可见的参数传递给reverse()方法一样。在reverse()内部,可以使用
yield
调用块:

def reverser
  return_value_of_block = yield
  return_value_of_block.reverse
end
编写
yield
,或
yield()
,就像编写
未修改的块方法()。换句话说,
yield
是ruby给block方法起的名字

如果该块应该接受参数,那么该块将如下所示:

result = do_stuff do |x|
  x * 2
end
def do_stuff
  return_value_of_block = yield(4)  #call block with the argument 4
end
do_stuff()将如下所示:

result = do_stuff do |x|
  x * 2
end
def do_stuff
  return_value_of_block = yield(4)  #call block with the argument 4
end
请问这个概念的名字是什么,这样我就可以读出来了

describe "reverser" do
it "reverses the string returned by the default block" do
  result = reverser do
    "hello"
  end
  result.should == "olleh"
end
end

def reverser(sent)
words = sent.split(" ")
  i = 0
  while i < words.length
   words[i] = words[i].reverse
   i += 1
  end
  words.join(" ")
end

搜索
ruby块

谢谢。我还看到了“yield”实现block.call比yield慢两倍。请注意这一点。正如我所写的:我的目标是可读性,并试图使我的解决方案易于理解。表演不是我的首要任务。谢谢。我还看到了“yield”实现block.call比yield慢两倍。请注意这一点。正如我所写的:我的目标是可读性,并试图使我的解决方案易于理解。表演不是我的首要任务。谢谢。我还看到了“yield”实现block.call比yield慢两倍。请注意这一点。正如我所写的:我的目标是可读性,并试图使我的解决方案易于理解。表演不是我的首要任务。谢谢。我还看到了“yield”实现block.call比yield慢两倍。请注意这一点。正如我所写的:我的目标是可读性,并试图使我的解决方案易于理解。性能不是我的优先级。这里不需要显式返回。这里不需要显式返回。这里不需要显式返回。这里不需要显式返回。这里不需要显式返回。