如何在rails上传递ruby中的参数

时间:2022-10-26 23:17:35

I have an issue with the code below, it takes no argument, and i am suppose to create a method to return the answer. I don't know how to get the value passed. And please what is the name of this concept, so i can read it up.

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

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

2 个解决方案

#1


0  

Your reverser takes a block which you call to get it return value:

您的反向器需要一个块,您可以调用它来获取它的返回值:

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

Note: You do not need the string variable. I just use it for readability reasons. You can also just write block.call.reverse.

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

#2


3  

def reverser
  yield.reverse
end

Here is the code in question:

这是有问题的代码:

result = reverser do
  "hello"
end

That is equivalent to:

这相当于:

result = reverser() do
  "hello"
end

Breaking that down:

打破这种情况:

    method call -+      +- 'do' marks the start of the block
                 |      |
                 V      V 
    result = reverser() do
      "hello"
    end
     ^
     |
     +--- end of the block

A block is like a method, and it's as if the block is passed as an invisible argument to the reverse() method. Inside reverse(), you can call the block using yield:

块就像一个方法,就好像块作为一个不可见的参数传递给reverse()方法。在reverse()内部,您可以使用yield调用块:

def reverser
  return_value_of_block = yield
  return_value_of_block.reverse
end

Writing yield, or yield(), is like writing unamed_block_method(). In other words, yield is the name ruby gives to the block-method.

写入yield或yield()就像编写unamed_block_method()一样。换句话说,yield是ruby给块方法的名称。

And if the block is supposed to take arguments, then the block will look like this:

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

result = do_stuff do |x|
  x * 2
end

And do_stuff() will look like this:

do_stuff()将如下所示:

def do_stuff
  return_value_of_block = yield(4)  #call block with the argument 4
end

And please what is the name of this concept, so i can read it up.

请问这个概念的名称是什么,所以我可以读一读。

Search for ruby blocks.

搜索红宝石块。

#1


0  

Your reverser takes a block which you call to get it return value:

您的反向器需要一个块,您可以调用它来获取它的返回值:

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

Note: You do not need the string variable. I just use it for readability reasons. You can also just write block.call.reverse.

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

#2


3  

def reverser
  yield.reverse
end

Here is the code in question:

这是有问题的代码:

result = reverser do
  "hello"
end

That is equivalent to:

这相当于:

result = reverser() do
  "hello"
end

Breaking that down:

打破这种情况:

    method call -+      +- 'do' marks the start of the block
                 |      |
                 V      V 
    result = reverser() do
      "hello"
    end
     ^
     |
     +--- end of the block

A block is like a method, and it's as if the block is passed as an invisible argument to the reverse() method. Inside reverse(), you can call the block using yield:

块就像一个方法,就好像块作为一个不可见的参数传递给reverse()方法。在reverse()内部,您可以使用yield调用块:

def reverser
  return_value_of_block = yield
  return_value_of_block.reverse
end

Writing yield, or yield(), is like writing unamed_block_method(). In other words, yield is the name ruby gives to the block-method.

写入yield或yield()就像编写unamed_block_method()一样。换句话说,yield是ruby给块方法的名称。

And if the block is supposed to take arguments, then the block will look like this:

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

result = do_stuff do |x|
  x * 2
end

And do_stuff() will look like this:

do_stuff()将如下所示:

def do_stuff
  return_value_of_block = yield(4)  #call block with the argument 4
end

And please what is the name of this concept, so i can read it up.

请问这个概念的名称是什么,所以我可以读一读。

Search for ruby blocks.

搜索红宝石块。