Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Homework: Array Play
[1] pry(main)> our_class=["Ben","Anna","Shirley","Lauren","Ben","Michelle","Angie","Matt"]
=> ["Ben", "Anna", "Shirley", "Lauren", "Ben", "Michelle", "Angie", "Matt"]
[6] pry(main)> our_class.each do |name|
[6] pry(main)* if name.length<5
[6] pry(main)* less_than5 << name
[6] pry(main)* end
[6] pry(main)* end
=> ["Ben", "Anna", "Shirley", "Lauren", "Ben", "Michelle", "Angie", "Matt"]
[7] pry(main)> less_than5
=> ["Ben", "Anna", "Ben", "Matt"]
[39] pry(main)> sentence = "Ruby is actually kind of fun once you get used to it."
=> "Ruby is actually kind of fun once you get used to it."
[40] pry(main)> sentence_array = sentence.split(" ")
=> ["Ruby",
"is",
"actually",
"kind",
"of",
"fun",
"once",
"you",
"get",
"used",
"to",
"it."]
[42] pry(main)> words4long = []
=> []
[43] pry(main)> sentence_array.each do |words|
[43] pry(main)* if words.length == 4
[43] pry(main)* words4long << words
[43] pry(main)* end
[43] pry(main)* end
=> ["Ruby",
"is",
"actually",
"kind",
"of",
"fun",
"once",
"you",
"get",
"used",
"to",
"it."]
[44] pry(main)> words4long
=> ["Ruby", "kind", "once", "used"]
[45] pry(main)>
[8] pry(main)> movies
=> [{:title=>"Forest Gump", :budget=>55, :stars=>["Tom Hanks"]},
{:title=>"Star Wars", :budget=>11, :stars=>["Mark Hamill", "Harrison Ford"]},
{:title=>"Batman Begins",
:budget=>150,
:stars=>["Christian Bale", "Liam Neeson", "Michael Caine"]},
{:title=>"Titanic",
:budget=>200,
:stars=>["Kate Winslet", "Leonardo DiCaprio"]},
{:title=>"Inception", :budget=>160, :stars=>["Leonardo DiCaprio", "JGL"]}]
[15] pry(main)> less100 =[]
=> []
[16] pry(main)> movies.each do |hash|
[16] pry(main)* if hash[:budget] <100
[16] pry(main)* less100 << hash[:title]
[16] pry(main)* end
[16] pry(main)* end
=> [{:title=>"Forest Gump", :budget=>55, :stars=>["Tom Hanks"]},
{:title=>"Star Wars", :budget=>11, :stars=>["Mark Hamill", "Harrison Ford"]},
{:title=>"Batman Begins",
:budget=>150,
:stars=>["Christian Bale", "Liam Neeson", "Michael Caine"]},
{:title=>"Titanic",
:budget=>200,
:stars=>["Kate Winslet", "Leonardo DiCaprio"]},
{:title=>"Inception", :budget=>160, :stars=>["Leonardo DiCaprio", "JGL"]}]
[17] pry(main)> less100
=> ["Forest Gump", "Star Wars"]
[18] pry(main)>
[20] pry(main)> star_leo =[]
=> []
[21] pry(main)> movies.each do |role|
[21] pry(main)* if role[:stars].include?("Leonardo DiCaprio")
[21] pry(main)* star_leo << role[:title]
[21] pry(main)* end
[21] pry(main)* end
=> [{:title=>"Forest Gump", :budget=>55, :stars=>["Tom Hanks"]},
{:title=>"Star Wars", :budget=>11, :stars=>["Mark Hamill", "Harrison Ford"]},
{:title=>"Batman Begins",
:budget=>150,
:stars=>["Christian Bale", "Liam Neeson", "Michael Caine"]},
{:title=>"Titanic",
:budget=>200,
:stars=>["Kate Winslet", "Leonardo DiCaprio"]},
{:title=>"Inception", :budget=>160, :stars=>["Leonardo DiCaprio", "JGL"]}]
[22] pry(main)> star_leo
=> ["Titanic", "Inception"]
[23] pry(main)>
[49] pry(main)> def four_words(text)
[49] pry(main)* new_array=[]
[49] pry(main)* text.split(" ").each do |word|
[49] pry(main)* if word.length ==4
[49] pry(main)* new_array << word
[49] pry(main)* end
[49] pry(main)* end
[49] pry(main)* new_array
[49] pry(main)* end
=> :four_words
[50] pry(main)> four_words("This had better work")
=> ["This", "work"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.