Categories
Dev Bootcamp

Introducing: Nearby Meetups

I just finished Phase 2 of Dev Bootcamp! It’s crazy how much I learned in the past 6 weeks. Git, Ruby, Sinatra, Javascript, AJAX, OAuth… all these terms were foreign to much just a few months ago.

I’m just writing here to share a simple Sinatra app that I built for my personal project during Phase 2.

“Nearby Meetups” started out as a fun project that would help me solidify EVERYTHING that I’ve learned during my first 6 weeks of DBC as well as learning more technologies.

I like Meetup.com, but the user interface didn’t appeal to me, so I rarely used it. I like to see things visually – where the events are located, how popular they are, when they are taking place, etc. – all at one glance.

Enter “Nearby Meetups” (www.nearbymeetups.com), a web app that utilizes the data from Meetup.com and visually displays them in a way that I would like them to. This was originally just a project that I did to solidify my learning, but after presenting it to everyone at DBC, I’ve received a lot of positive feedback and request to open it up to the public so that they can use it, too.

I am open to all kinds of feedback, issues, suggestions, and feature requests – anything that will help improve this app and make it easier for you to use.

Anyway, I’m ready to go onto Phase 3 and finally learn Rails on my last few weeks here!

Categories
Dev Bootcamp

A Tear-Down of the Enumerable map Method

What does map do?

Here is the map method:

map { |item| block }

And here is what it returns:

#=> new_array

Essentially, map is an Enumerable method that runs a code block once for each element of the object and returns a new array of of the results of the block.

If you’re just starting to learn programming, you may be asking, “what does this really mean?” Let me try to break it down.

Enumerable provides collection classes with several searching and sorting methods. Arrays and Hashes are two examples of classes that have Enumerable properties, and therefore, can use Enumerable methods. map is one of those Enumerable methods.

What does it really mean? I work out.

Here is an example senario and code:

benched_weights = [90,140,160,120,165]
benched_weights.map { |wt| wt + 45 } 
#=> [135,185,205,165,210]

One the first line of the code, you’re keeping track of how much weight you are putting onto the barbell when you bench on chest days. You are storing this in an array called benched_weights. On the first day, you put 90lbs on the bar. On the second day, you put 140lbs on the bar. Third day, 160lbs. You had a bad day on the fourth day and only put 120 lbs on the bar. And so forth…

Now, let’s say, you want to see how much weight you are benching in total, including the weight of the barbell itself. The barbell weighs 45lbs. On the second line if the code, you used the map method. You assigned the variable wt in between the pipes as a placeholder for each element in the Array and then a code block that says wt + 45. What the code is doing here is going through each weight that you logged and adding 45 to it.

During the first pass, wt is equal to the first element of the array, which is 90, and then returns wt + 45, which is 135. After the first pass, it loops to the second element, which is 140, and then returns wt + 45, which is 185. Third pass, the wt is 160 and returns wt + 45, which is 205, and so on until it goes through each element in the given array.

When all is said and done, the map method will return all of the values given by the code block in the form of a new Array, which is shown on the third line.

As a side note, you can also use Enumerable#collect interchangeably with #map. They are completely synonymous (i.e. benched_weights.collect { |wt| wt + 45 }.

How does this compare to “each”?

Many beginner’s Ruby books and courses use the each method as an introduction to teach iterators and code blocks. If you are familiar with the Array each method, you’ll notice that the Enumerable map method is very similar to it. Both methods calls the code block once for each element in the object. The key difference between the two methods stop here.

After the each method runs the block for each element, it stops and then returns nil. The map method, on the other hand, stores the results that the code block return for each element into a new array and then return the new array after the method is finished. Using the map method can save you several lines of code if you’re making the each method store the results in a new array. It can also be useful in many other ways.

If you have any questions or what further clarification, please leave a message in the comment box below.

This blog has been initially published on tonymai.github.io.