ruby数组带参数?

时间:2014-04-28 11:26:01

标签: ruby arrays

我有这样的数组

month_string = [" Jan"," 2月"," 3月"," 4月"," 5月" "君""七月""八月""九月""十月""十一月""减速"]

我想从五月到另一个。

take将使用单个param。其他简单方法来执行此操作。

输出需要:  ["五月""君""七月""八月""九月"]

1 个答案:

答案 0 :(得分:3)

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" )