簡體   English   中英

紅寶石數組帶參數?

[英]ruby array take with parameters?

我有這樣的數組

month_string = [“ Jan”,“ Feb”,“ Mar”,“ Apr”,“ May”,“ Jun”,“ Jul”,“ Aug”,“ Sep”,“ Oct”,“ Nov”,“ Dec” ]

我想從五月到九月。

take可以與單個參數一起使用。其他任何簡單的方法也可以。

輸出必須為:[“ May”,“ Jun”,“ Jul”,“ Aug”,“ Sep”]

month_string[ month_string.index( "May" )..month_string.index( "Sep" ) ]

您還可以創建函數。

def month_set( months, a, b )
    months[ months.index(a)..months.index(b) ]
end

month_set( month_string, "May", "Sep")

甚至將其包含在函數中

def month_set( a, b )
    months = [
        "Jan","Feb","Mar","Apr","May","Jun",
        "Jul","Aug","Sep","Oct","Nov","Dec"
    ]

    months[ months.index(a)..months.index(b) ]
end

month_set( "May", "Sep" )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM