Showcasing the Normal, Unaltered Use of the Params Hash

The params hash is a “rack” object that is accessible through Sinatra’s request object. This params hash stores information posted by html forms in the form of -you guessed it – a hash, which contains a key-value pair. The key of this pair defaults to the value of the <input>’s name=“attribute”. Meanwhile, the info that the user submits is the value of that key-value pair.

1
2
3
4
<form action='/' method="POST">
     <label>Location</label>
          <input name="location"/>
     <input type="submit">Submit</input>

For the above html, you can access the user’s entry in app.rb within the post '/' do route:

1
2
3
4
post '/' do
  @location = params[:location]
  "Ain't #{@location} grand?"
end

If I entered “New York City” into the <input name="location"/>, then @location = "New York City". Similarly, if there was another input, <input name="arrival_date">, where I entered “June,” then the params hash would have two key-value pairs.

1
params == {:location => "New York City", :arrival_date => "June"}

Kick it up a Notch with a Nested Params Hash

Amazingly, you can modfiy the data structure of the params hash by carefully altering the name attribute of <input>. For example, you can make the params hash contain an array of hashes. Because this process is slightly complex, I will first show off what’s possible with an example, then list further below the key points for understanding how to do it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<h1>List of Games with Name, Difficulty, and Number of Players</h1>
<form action='/games' method="POST">
  <% 5.times do %>
    <fieldset>
      <li>
        <label>Name</label>
        <input ... name="game[][name]"/></li>
      <li>
        <label>Difficulty</label>
        <input ... name="game[][difficulty]" />
      </li>
      <li>
        <label>Players</label>
        <input ... name="game[][players]" />
      </li>
    </fieldset>
  <% end %>
  <input type="submit"/>
</form>

The above form outputs the following params hash:

1
params == {"game"=>[{"name"=>"Connect 4", "difficulty"=>"Easy", "players"=>"2"}, {"name"=>"Go Fish", "difficulty"=>"Easy", "players"=>"4"}, {"name"=>"Monopoly", "difficulty"=>"Easy", "players"=>"4"}, {"name"=>"Chess", "difficulty"=>"Hard", "players"=>"2"}, {"name"=>"Settlers of Catan", "difficulty"=>"Medium", "players"=>"4"}]}

As you can see, the params hash’s top-layer has only one key-value pair, with “game” as its key. If you look at the html form, this initially makes sense because game is repeated as the top-layer for all three inputs.

As programmers, we then have a choice of modifying the value of this pair from a string into something else. If we want to turn the pair’s value into an array, then we write an empty array as above. If we want to make the value a hash, then we surround the attribute in square brackets.

Notice that each hash in the array contains 3 key-value pairs. Just like the first example with “arrival_date” and “location,” adding a key-value pair on the same layer will add a key-value pair to that same hash – not create another hash inside a hash.

At this point, your brain might be wheeling a bit. Hopefully these rules below will help you clarify how to alter the params hash through html forms.

Primer for Understanding Params

  1. Realize that because the params hash stores form data, the user’s entry should always be the value of a key-value pair.

  2. If you think of params hash as having a pointer to what will be returned, then everytime you add an extra layer, the data type of the extra layer – be it array or hash, will be the return of the formerly-lowest layer.

    • In other words, <input name="game[][name][]" /> sets the value of the :name=>value pair to an array, and although this wouldn’t break your app, it doesn’t make that much sense because there would be no way to track the attributes of a particular object. Every entry would be added to the array, rather than the array containing hash elements.

    • Therefore you must end it with a key-value pair like <input name="game[][name][][syllables]" />. Apologies for the quirky example, but here the user would input the number of syllables in the game’s name, of which there are many: such as “ping pong,” “table tennis,” and “pong.”

  3. By convention, you leave the top-layer of the params hash as being without square brackets, despite the fact that it is a key-value pair inside the params hash.

  4. When in doubt, practice using the following method in irb:

1
2
3
4
require 'rack'
def p(params)
 Rack::Utils.parse_nested_query(params)
end

and use the following syntax: p("game[][name][][syllables]" )

Now as a challenge, can you decipher what the above params hash looks like?

Answer: params == {:game => [{:name=> [ {:syllables=> value} ] }] }

Seth Godin came and talked to the Flatiron School today. This is what I observed:

1. Seth’s Remarks were Catered to the Flatiron School

His opening remarks declared every Flatiron student as being at the right place at the right time. Just like his dad who earned a degree in Mechanical Engineering in 1957, corporations are picking us straight off from campus. The question Seth posed is not whether we will be successful, but whether we will matter.

2. Seth Answered Questions in One of Two Manners

a. He unpacked meandering concerns

When a teaching assistant asked how to know how to apply themselves (i.e. what does it mean to “matter”) while they’re waiting for the next big idea, Seth Godin broke down the concern into several parts. First, he asserted the context in which the concern takes place: anyone can start an online business in 15 minutes, but that’s not what it means to matter. Most technical ideas are not anything that Flatiron students couldn’t figure out. For instance, once Twitter became big, dozens of Twitter clones came out because the technical part of making it was easy. To matter means breaking from convention, experiencing fear, and taking the critques of naysayers – who arrive at the pre-party of any “next big thing.”

Then, he addressed the lingering concern of waiting for the next big idea: it’s bullshit. If you are waiting, then you are hiding. What really matters isn’t the idea itself – Altavista existed years before Google did as a search engine. Google was not the first company to place text ads next to searches. What made Google unique was their perspective, philosophy, and energy. That’s what people bought into, and that’s the platform through which Google changed its users.

b. He pivoted the question’s intent to deliver his value to his audience

A Flatiron student asked how, given Seth’s advice that we should use fear as an indicator that we should keep pushing, if there were strategies for changing people who lacked confidence in themselves as opposed to people who’ve had some experience of success and therefore have the foundation for overcoming their fears. Seth related a story about his mentor Zig Zigar who corrected Seth on his public-speaking habit of launching his full energy at re-engaging a sleeping audience member. Zig said that he focused on the two hundred other audience members who came to the event for the right reason and who would benefit from his talk.

“Shun the non-believers,” Seth said. A Google spokesperson said more than 8 years ago that one day, everyone would use Google, so the later people found out about it – the better – because then the product would be so much better. Rather than focusing on the people who don’t want to listen to you, Seth said first convert all the people who are listening, then worry about the others afterwards. Seth managed to avoid answering the question directly – while legitimately providing his own answer – that focusing on changing people who won’t listen is a terrible use of resources. Rather, sell to people who want to be sold to.

3. Seth synthesizes his philosophy through every response

Seth embodies his philosophy in his talks. He draws from common everyday stories, and has an aim for them – to puncture our illusions about how the world works and the status of our own safety. It is not safe to conform because the conformers will not be picked for once-in-a-lifetime opportunities. Conformers are not “best in the world,” and they won’t “matter.” To matter, you face fear everyday so that you do not become a success story that does the same thing till old age.

For Seth Godin, this means writing a blog post every morning that may fail, going to an intimate talk with no notes, and taking a hiatus from writing books to building projects – a task outside his comfort zone. Every piece of advice Seth issues is derived from real-life insight from implementing his own suggestions in frightful situations. Seth matters to others because he takes on the task of facing the resistance, inviting it to tea, and bringing back a nugget of wisdom to guide others who dare to take a trail-blazing path.

Thanks Seth for the great talk. It was relevant and full of empathy.

How will you learn to program? What you learn first can determine your future as a programmer. As a novice, I can only speak to my experience. Here was my thinking process:

I’d like to build useful and interesting projects as quickly as possible, while also making a living from my ability to program. After learning on my own and making steady but not lightning-fast progress, I looked for an immersive schooling program that would speed up my learning. The school I chose, the Flatiron School, teaches students in 4 months to be capable web programmers. Their curriculum prepares students to be Ruby on Rails web developers, with all students finding employment after graduation. This is exactly what I was looking for – fast and effective.

So what do you learn in their curriculum?

  1. Basic Vocabulary – a web developer works both in visual space and data manipulation. Flatiron School’s pre-work begins with a quick grounding in how websites work and the concepts professionals use to design them.

  2. HTML – Just as Twitter uses tags to describe content, “Hypertext Markup Language” is used to describe the content of the Web. For example, a paragraph of text would be surrounded by a paragraph tag, and an image would be surrounded by an image tag. Using HTML makes it easy for search engines to figure out what a website is about, and it also helps web developers adjust the layout of a website.

  3. CSS – “Cascading Style Sheets” work with HTML to adjust the visual design of a website. For example, CSS can tell all paragraphs to appear in 14 point Helvetica font and to start as gray, but to fade to black when a user’s mouse hovers over the text. CSS makes it easier to visually design a site because it separates content from style, allowing for web developers to think about these two topics separately. Furthermore, web developers can overhaul the style of a site very quickly using CSS – rather than changing the style of every paragraph in a large website containing thousands of articles, a web developer can change just one document that will then apply that style to all paragraphs of every article.

  4. Javascript – This is the programming language of the internet. It is used to listen for events and change the display on a website based on those events. Javascript is a programming language because developers can use it to instruct computers to make new methods that do entirely new things. By contrast, HTML and CSS are not programming languages because developers describe content with a set vocabulary, which web browsers read and apply a set of fixed methods to achieve their effects.

  5. Ruby – This is the programming language of Ruby on Rails. It is known for being intuitive to write and, when combined with Rails – to be a fast and effective way to make websites compared with other languages.

  6. Rails – This is an open source web application framework and server, which means that it is a standardized way to create a website. A website has a lot of files that interact with each other, and Rails is a popular way of having those files interact. It’s popular because many of the decisions for how website files interact is defaulted according to an easily-deciphered logic, allowing for developers to focus their mental efforts on adding value.

  7. The Command Line – computers can receive instructions through “the command line.” This is the standard way for programmers to interact with computers because many commands can only be issued through the command line.

  8. SQL – “Structured Query Language” is a popular database language. Databases store data in an easily retrievable format. When a website like Facebook displays the status updates of all your friends, it has to query that data before displaying it. Whereas it would take you or me dozens of hours to find that information in a stack of papers, databases are designed to make that process quick and accurate.

  9. Testing – while not technically requiring its own programming language, testing does use a distinct vocabulary and methodology. Because a website has a lot of moving parts, programmers create tests that let them identify which part suddenly breaks down with any change in the code.

  10. Best Practices – Just as in any discipline, there are some problems that have been “figured out.” These best practices are important to know because they are the best way to solve frequently occurring problems.

I dabbled in programming for a year before deciding to become a programmer. The most important insight for me during this time was figuring out when to take programming seriously. If you want to find out if you’re interested, then dabble by all means. However, if you’re aiming to be capable as a programmer, then the sooner you get serious, the better – you’ve got a lot of work to do.

After being inspired by Flatiron students who created a shitavisays gem, I decided to prepare myself now to make a gem in the future. My first step is reading this presentation, which gives a brief overview of the gem-cutting process. See below for my own quick summary.

Introduction (Slides 1-13)

Speaker Nick Quaranto introduces himself and his presentation.

Gem* Definition (Slides 14-22)

Gems are a packed Ruby library or application. RubyGems help you download, install, and manipulate them.

Gem Content (Slides 23-44)

Gems have three things: code, docs, and a gemspec.

Code are the methods that the gem adds to Ruby. Docs describe the code’s functionality or aid the gem without being code. A gemspec specifies the required gems for the code to work.

To access a gem’s code in Ruby, use RubyGems to override the Ruby’s standard $LOAD_PATH. This means a gem’ code, such as found in a bin/ or lib/ folder of that gem, will be put into the $LOAD_PATH.

There’s a difference between .gem an gem files. I’m not sure at the moment, but it seems that a gem is a subset of .gem because while a gem is a packed Ruby library, a .gem includes other data or metadata, both of which need to be unzipped from a compressed file format.

Why Make Gems (Slides 45-53)

Reuse your code, practice coding with interesting projects, and help others.

How to Make a Gem (Slides 54-72)

Locally, you build and install it. Remotely, you push then install.

See above for a Gem directory structure, some example code, its Gemspec, and the Gem build command.

LOCALLY

Build

You need to write a Gemspec with metadata on your gem and code files that contain the methods of your Gem.

Install

After you install your gem, you should test it.

REMOTELY

Push and Install

$gem push gem_name-0.0.0.gem will let you push it to RubyGems.org. Then you can install and run it.

Command Line and Gems (Slides 74-84)

Use the bash commands and a little C to modify and improve your Gem! I did not understand exactly how, but here the speaker extends the functionallity of his Gem by letting it take any string as a parameter.

Testing Resources (Slides 85-95)

Test because you are releasing your Gem to the public, and you will be scrutinized.

Documentation (Slides 95-105)

Go to http://yarddoc.org to learn how to document your code. Documentation helps others use your gem. Also, before pushing your gem, strongly consider saying hi to @rubygems on Twitter because webhook allows the rubygems community to scrutinize your gem the moment it’s published, so it’s a good idea to say hi to your future scrutinizers.

Classic Pointers for Gem-Cutting (Slides 106-151)

What to do over the lifetime of a gem.

Make your Gem Accessible

Write a README, use a license so your code isn’t copyrighted, name your gem properly and version it using a major, minor, and patch scheme.

Make your Gem Work

Organize your file structure the right way by having only one top-level file. Also, specify your dependecies by being neither too optimistic or exact with the versions that you expect to work. Instead, specify a range, such all minor gem versions < 1.0.0.

Be Classy

Cut prereleases of your gem so that people who install it have the expectation that it might still have bugs. Furthermore, use continuous integration to constantly test your code, which you can do for free at https://travis-ci.org/.

Gem-cutting Takeaways

  1. Making a gem is pretty straightforward and rewarding
  2. To develop confidence, look over the presentation and make the easiest gem
  3. Prepare for feedback to your gem after you push it

What is a branch?

Your local copy of the repository is the branch. You might often be on your master branch.

What is an origin?

By convention, this is the name of your remote repository.

The difference between Branch and Origin

Remotes are the entire repository, which includes all branches. Your master branch is more like a unit of work within the entire project that is the remote.