[Ruby] ActivitySupport
Arrays
Implement the last_games
method below to return the games from the passed index to the end of the list. Try usingArray#from
to return all games starting from 'Contra'. Also change the call to last_games
to pass in the correct index
.
def last_games(games, index) games.from(index) end games = ["Super Mario Bros.", "Contra", "Metroid", "Mega Man 2"] puts last_games(games,1)
Arrays - Part 2
We have a last_games
method, but we need a first_games
method as well. Use Array#to
to return the list of games up to Metroid. Also change the call to first_games
to take in the correct index.
def first_games(games, index) games.to(index) end games = ["Super Mario Bros.", "Contra", "Metroid", "Mega Man 2"] puts first_games(games, 2)
Dates
Implement the anniversary
method below to return a DateTime
representing the given number of years
after the game's release
date.
def anniversary(game, years) game[:release].advance(years: years) end game = { name: 'Contra', release: DateTime.new(1987, 2, 20, 0, 0, 0) } puts anniversary(game, 20)
Hashes
Using ActiveSupport
, return the difference between Mario's favorite games and Luigis's favorite games by implementing the difference_between
method.
def difference_between(player1, player2) player1.diff(player2) end mario_favorite = { sports: "Mario Sports Mix", action: "Super Mario World" } luigi_favorite = { sports: "Golf", action: "Super Mario World" } puts difference_between(mario_favorite, luigi_favorite)
Hashes - Part 2
Implement the exclude_character
method below to return characters except the passed in character. Use ActiveSupport to return these key/pair values. Also, change the call to exclude_character
so that yoshi's games are excluded.
def exclude_character(games, character) games.except(character) end games = { mario: ["Super Mario World", "Super Smash Bros. Melee"], luigi: ["Luigi's Mansion"], yoshi: ["Yoshi's Island", "Yoshi's Story"] } puts exclude_character(games, :yoshi)
Numbers
Refactor the describe_count
method below to use ActiveSupport in order to find out if a number is even or odd.
def describe_count(games) if games.empty? "You have no games" elsif games.length.even? "You have an even number of games" elsif games.length.odd? "You have an odd number of games" end end games = ["Super Mario Bros.", "Contra", "Metroid", "Mega Man 2"] puts describe_count(games)
Strings
Implement the convert_title
method to use one of String's core extension methods. Given the input below, this method should return the string 'Super Mario Bros.'
def convert_title(title) title.titleize end puts convert_title("super mario bros.")
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具