日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

splat net_Ruby中的Splat参数

發布時間:2025/3/11 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 splat net_Ruby中的Splat参数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

splat net

Ruby Splat參數 (Ruby Splat Arguments)

We have learnt how to work with methods in Ruby? We are very well aware of the fact that methods may or may not consume any arguments. Let us discuss the methods which consume argument or have a predefined argument list. This argument list may contain any number of arguments but with a compulsion that you will have to pass the same number of arguments with the method as specified in the argument list of the method. But this decreases the versatility of the code, what if you have created a method which adds three numbers but the requirement of the time is to add two numbers only. Your function will fail in this case. One way is to overload the method but the thing is how many times you can overload the method because there may be n number of possibilities and that will eventually make your code redundant. Ruby has a good concept of splat arguments which allows you to pass any number of arguments in the method. Splat arguments are of two types which are discussed in the rest of the article.

我們已經了解了如何在Ruby中使用方法? 我們非常了解以下事實:方法可能會或可能不會使用任何參數。 讓我們討論消耗參數或具有預定義參數列表的方法。 該參數列表可以包含任意數量的參數,但具有強制性,您必須使用方法的參數列表中指定的方法傳遞相同數量的參數。 但是,這降低了代碼的通用性,如果您創建了一個將三個數字相加但方法是僅將兩個數字相加的方法,該怎么辦。 在這種情況下,您的功能將失敗。 一種方法是重載該方法,但問題是您可以重載該方法多少次,因為可能存在n種可能性,最終將使您的代碼冗余。 Ruby具有splat參數的良好概念, 它允許您在方法中傳遞任意數量的參數 。 Splat參數有兩種類型,本文其余部分將進行討論。

1)單個Splat參數 (1) Single Splat Arguments)

Single splat argument is implemented with the help of * operator. You can pass Arrays with the help of them. Refer the syntax given below,

單個splat參數在*運算符的幫助下實現。 您可以在它們的幫助下傳遞數組。 請參考下面給出的語法,

def method_name( *args)...end

The following example will help you to understand its implementation,

以下示例將幫助您了解其實現,

def mul(*args)pro = 1args.each do |ar|pro = pro * arend return pro endputs "Product is #{mul(12,44,55,33,22,55)}" puts "Product is #{mul(1.2,4.4,5.5,3.3,2.2,5.5)}" puts "Product is #{mul(1,2)}" puts "Product is #{mul(100,45)}"

Output

輸出量

Product is 1159567200 Product is 1159.5672000000002 Product is 2 Product is 4500

You can observe in the above code that we have created a method that is multiplying all the arguments which are passed in it. We are taking help from a splat operator which is allowing us to pass as many arguments we want. We have passed the different number of arguments in each method call. If this is not the case then we might have to define the different methods for the different number of arguments.

您可以在上面的代碼中觀察到,我們已經創建了一個方法,該方法將傳遞給它的所有參數相乘。 我們正在從splat運算符獲取幫助,該操作符允許我們傳遞所需的盡可能多的參數。 我們在每個方法調用中傳遞了不同數量的參數。 如果不是這種情況,那么我們可能必須為不同數量的參數定義不同的方法。

2)Double splat參數 (2) Double splat arguments)

The concept of double splat argument was introduced in Ruby 2.0. The implementation is pretty similar to a single splat argument but an add-on feature that will also work for hashes. It is implemented with the help of ** operator. Following is the syntax that will tell you how can we use double splat arguments.

在Ruby 2.0中引入了double splat參數的概念。 該實現與單個splat參數非常相似,但是其附加功能也適用于哈希。 它在**運算符的幫助下實現。 以下是語法,它將告訴您如何使用雙splat參數 。

def show( **args)...end

Now, let us go through its example for understanding it better.

現在,讓我們通過其示例來更好地理解它。

def fruits (**fruits_and_color)fruits_and_color.each do |frt, color|puts "Fruits: #{frt}"puts "Color: #{color}"end enddata1 = {"Kiwi": "Green","Peach": "Pink","Banana": "Yellow","Grapes": "Green" }data2 = {"Kiwi": "Green","Peach": "Pink","Banana": "Yellow","Grapes": "Green","Watermelon": "Blue" }fruits data1 fruits data2

Output

輸出量

Fruits: Kiwi Color: Green Fruits: Peach Color: Pink Fruits: Banana Color: Yellow Fruits: Grapes Color: Green Fruits: Kiwi Color: Green Fruits: Peach Color: Pink Fruits: Banana Color: Yellow Fruits: Grapes Color: Green Fruits: Watermelon Color: Blue => {:Kiwi=>"Green", :Peach=>"Pink", :Banana=>"Yellow", :Grapes=>"Green", :Watermelon=>"Blue"

In the above code, you can observe that we have created two hashes data1 and data2. We have created a method fruits and we are passing these hashes with the different number of entries into the method.

在上面的代碼中,您可以觀察到我們創建了兩個哈希data1和data2 。 我們創建了一個方法結果 ,并將這些具有不同條目數的哈希傳遞給該方法。

翻譯自: https://www.includehelp.com/ruby/splat-arguments.aspx

splat net

總結

以上是生活随笔為你收集整理的splat net_Ruby中的Splat参数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。