Skip to content

Instantly share code, notes, and snippets.

@mykiy
Last active April 4, 2018 11:57
Show Gist options
  • Save mykiy/e5fa88ea1bae29b6c13178612cd37a92 to your computer and use it in GitHub Desktop.
Save mykiy/e5fa88ea1bae29b6c13178612cd37a92 to your computer and use it in GitHub Desktop.
arr = [1,2,3,4,5]
arr.each { |n| n + 2 }
arr
=> [1,2,3,4,5]
arr.map! { |n| n + 2 }
arr
=>
[3,4,5,6,7]
arr.collect! { |n| n + 2 }
arr
=>
[3,4,5,6,7]
arr.select!{ |n| n > 2 }
arr
=>
[5,6,7]
arr.sum
=>
18
arr = [1,2,nil,8,nil}
arr.compact!
=>
[1,2,8]
["a", "b"].concat( ["c","d"] )
=>
["a","b","c","d"]
------
hash
-----
my_hash = Hash.new
my_hash = {}
my_hash.new(0)
my_hash[:name] = "velu"
my_hash[:age] = 22
movies = {
Ravanan: 4.8,
Thanioruvan: 4.5,
payanangal: 4
}
puts "What would you like to do?"
puts "-- Type 'add' to add a movie."
puts "-- Type 'update' to update a movie."
puts "-- Type 'display' to display all movies."
puts "-- Type 'delete' to delete a movie."
choice = gets.chomp.downcase
case choice
when 'add'
puts "What movie do you want to add?"
title = gets.chomp
if movies[title.to_sym].nil?
puts "What's the rating? (Type a number 0 to 4.)"
rating = gets.chomp
movies[title.to_sym] = rating.to_i
puts "#{title} has been added with a rating of #{rating}."
else
puts "That movie already exists! Its rating is #{movies[title.to_sym]}."
end
when 'update'
puts "What movie do you want to update?"
title = gets.chomp
if movies[title.to_sym].nil?
puts "Movie not found!"
else
puts "What's the new rating? (Type a number 0 to 4.)"
rating = gets.chomp
movies[title.to_sym] = rating.to_i
puts "#{title} has been updated with new rating of #{rating}."
end
when 'display'
movies.each do |movie, rating|
puts "#{movie}: #{rating}"
end
when 'delete'
puts "What movie do you want to delete?"
title = gets.chomp
if movies[title.to_sym].nil?
puts "Movie not found!"
else
movies.delete(title.to_sym)
puts "#{title} has been removed."
endmovies = {
Ravanan: 4.8,
Thanioruvan: 4.5,
payanangal: 4
}
puts "What would you like to do?"
puts "-- Type 'add' to add a movie."
puts "-- Type 'update' to update a movie."
puts "-- Type 'display' to display all movies."
puts "-- Type 'delete' to delete a movie."
choice = gets.chomp.downcase
case choice
when 'add'
puts "What movie do you want to add?"
title = gets.chomp
if movies[title.to_sym].nil?
puts "What's the rating? (Type a number 0 to 4.)"
rating = gets.chomp
movies[title.to_sym] = rating.to_i
puts "#{title} has been added with a rating of #{rating}."
else
puts "That movie already exists! Its rating is #{movies[title.to_sym]}."
end
when 'update'
puts "What movie do you want to update?"
title = gets.chomp
if movies[title.to_sym].nil?
puts "Movie not found!"
else
puts "What's the new rating? (Type a number 0 to 4.)"
rating = gets.chomp
movies[title.to_sym] = rating.to_i
puts "#{title} has been updated with new rating of #{rating}."
end
when 'display'
movies.each do |movie, rating|
puts "#{movie}: #{rating}"
end
when 'delete'
puts "What movie do you want to delete?"
title = gets.chomp
if movies[title.to_sym].nil?
puts "Movie not found!"
else
movies.delete(title.to_sym)
puts "#{title} has been removed."
end
else
puts "Sorry, I didn't understand you."
end
else
puts "Sorry, I didn't understand you."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment