我必须构建一个接受数组和字符串的方法。该方法应该返回一个包含三个元素的数组。它在索引0处的元素应该是字符串参数,并且它不应该包含数组参数的最后一个元素。
对于数组:
best_teams = ([Boston Celtics, LA Lakers, Chicago Bulls]
和字符串:
my_team = ("Utah Jazz")
我期待输出:
[Utah Jazz, Boston Celtics, LA Lakers]
我试过了:
def teams(best_teams, my_team)
sec = my_team.split + best_teams
return sec - sec[3].split
end
但它似乎不起作用。有什么想法吗?
答案 0 :(得分:0)
看,假设以下值:
best_teams = ['Boston Celtics', 'LA Lakers', 'Chicago Bulls']
my_team = "Utah Jazz"
def teams(best_teams, my_team)
# sec = nil
sec = my_team.split + best_teams
# the values at this point are
# sec = ["Utah", "Jazz"] + ['Boston Celtics', 'LA Lakers', 'Chicago Bulls']
# your single team name now become in an array with two values
# finally
# sec = ["Utah", "Jazz", 'Boston Celtics', 'LA Lakers', 'Chicago Bulls']
return sec - sec[3].split
# ["Utah", "Jazz", 'Boston Celtics', 'LA Lakers', 'Chicago Bulls'] - ["LA", "Lakers"]
# ["Utah", "Jazz", "Boston Celtics", "LA Lakers", "Chicago Bulls"]
end
请检查 https://apidock.com/ruby/String/split。 真正的问题是我猜你不知道如何拆分工作