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

 

posted @   Zhentiw  阅读(302)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· 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工具
点击右上角即可分享
微信分享提示