Organizing a summer camp in Ruby.

Summer is here and I’ve got a business idea, a summer camp for teenagers, but I have one problem: I communicate only in Ruby.
Lets see if I’m gonna be able to run my camp.

I created a great looking advertisement and the first kid came in to join:

I have to see the kid’s age to make sure he is old enough. Pretty easy:

John is happy to join my camp and told me about his hobbies. Great information. I can organize some activities and an entertainment in my camp based on kids hobbies. Let me add it.

So this is how John looks now:

Next day few more teenagers joined my camp and I had to modify my kid hash into array of hashes:

After several days I had enough kids joined and I started my camp.

I can see hobbies of every kid, for example John:

I just curious who has the most amount of hobbies. To find that out I need to iterate trough all kids and count how many hobbies each kid has, store that data in a new array, then find a kid whose amount of hobbies is equal to maximum number in array:

What activities should I organize for the kids? If I take them to a hike are they going to enjoy it? I need to see how many kids like hiking:

Wow! Only one Alex! Hiking is not a good idea then. Let me find out what is the most popular hobby kids have:

I see that most kids like soccer. But before we go playing it let me break down this code:

First step: collecting all hobbies into a new array:
kids.each {|kid| array << kid[:hobby]}

=> array=[[“Swimming”, “Reading”, “Soccer”], [“Hiking”, “Reading”], [“Dancing”, “Skiing”, “Baking”, “Cartoons”], [“Drawing”], [“Card-games”, “Soccer”], [“Soccer”, “Reading”, “Cartoons”]]

As we can see the result is an array of arrays. I used .flatten method to modify it into one single array.

array = array.flatten

=> [“Swimming”, “Reading”, “Soccer”, “Hiking”, “Reading”, “Dancing”, “Skiing”, “Baking”, “Cartoons”, “Drawing”, “Card-games”, “Soccer”, “Soc er”, “Reading”, “Cartoons”]

Then I iterated on that array and created out of it a new hash that have a hobby as a key and amount as a value:

array.each do |i|
if hobbies[i]
hobbies[i]+=1
else
hobbies[i]=1
end
end

I hope all kids are going to enjoy a soccer game in my camp and I hope my summer camp helped someone better understand how to work with arrays and hashes.

--

--

--

Fullstack web developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Golang | Javascript Syntax Insights

Explanation of Phrases in Testnet Accounting Menu

Ready for the Next Gen Cloud-Based Data Backup and Recovery?

Trip Report: ACCU 2018

DataBricks Delta Lake

Amazon Web Services (AWS)

Part 5 — Learning Flutter, Navigating

HTTP/1.1, HTTP/2, and HTTP/3: A Comparison

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Yulia Y.

Yulia Y.

Fullstack web developer

More from Medium

Authentication on Rails set up

Introducing Cordova 11 support in Monaca

yield(experience, “acquired”)

Only One Earth winners revealed