Ruby part 4, active support: dates, hashes, strings

Ruby part 4, active support: dates, hashes, strings

Active support

to intall it

$ gem install activesupport
$ gem install i18n

to load it

require 'active_support/all'


Some core extension: Date

mybirthday = Datime.new(2000,12,12,12,12,12)
# december 12, 2012, 12:12:12 hrs
mybirthday.at_beginning_of_day
#december 12, 2012 00:00:00
mybirthday.at_end_of_month
#december 31 23:59:59
mybirthday.at_beggining_of_year
#january 01, 00:00:00

Core extension: hash

options = {name: 'heriberto', age: '25'}
new_options = {name: 'heriberto', age: '25', address: 'cohuila 1608'}

Diference between two hashes

options.diff(new_options)

Turns keys into strings

options.stringify_keys
#{'name' => 'heriberto', 'age'=>'25'}

Using reverse_merge

options = {name: 'heriberto', age: '25'}
new_options = {name: 'clau', age: '19', address: 'cohuila 1608'}
options.reverse_merge(new_options)
#this will generate
options = {name: 'heriberto', age: '25', address: 'cohauila 1608'}

Removing one key

options = {name: 'heriberto', age: '25', address: 'cohauila 1608'}
options.except(:address)
#this will be the result
 {name: 'heriberto', age: '25'}

Even and odd numbers

puts “Impar” if number.even?#2,4,6,8,10,12,14,16
puts “Par” if number.odd?#1,3,5,7,9,11,13,15,17
def background-class(number)
  return 'white' if number.even?
  return 'gray' if number.odd?
end
tweets.each_with_index do |tweet, index|
   <div class='#{background-class(index)}'>"#{tweet}"</div>
end

Using Inflector

"#{1.ordinarize} Place"
#this will print “1st Place”
"#{23.ordinarize} Place"
# This will print “23rd Place”

Singularize

"woman".singularize
# print “Woman”
"Woman".pluralize
# print “Women”

Some exercise to practice a little(Exercises taken from codeschool.com)
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)

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 => 20)
  #or we can add
  #game[:release] + var.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

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

No Comments

Post A Comment