File download was a bit more complex. While an individual user would only upload one file to be associated with an assignment, the person who assigned the assignment would probably want a way to download all of the submitted work at once. For this, we implemented a gem called RubyZip. If you’re writing a rails app, you don’t need to require ‘rubygems’, but you DO need to require ‘zip/zip’ in your application.rb file.
The RubyZip documentation(http://rubyzip.sourceforge.net/) is pretty dense, but thorough. With a few modifications, we passed it an array of files, specified the file names, and created a file tree structure so that when the user downloaded the files, they were organized by submitter and named appropriately.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Everything worked on our local host, but when we deployed to our server we got an Nginx 403 permissions error. Once we SSH’ed into the server, we saw that the file was being created, but while the owner of the file was allowed to read and write the file, no one else could do anything with it.*
Much like you can run chmod in your bash window, Ruby also contains a chmod command. By adding one line to our code, we modified our permissions and allowed the user to download the file appropriately.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
Returning to the server, you can see that the permissions were appropriately modified, and the user can now download the Zip file of all the files for an assignment. The built-in site functionality works as well.
This is a great explanation of file permissions in Unix. For purposes of simplicity, I’ve abstracted away the routing and controller functionality that calls the above method.
]]>When we were designing our student pages at the Flatiron School, one of our major questions was what to include. As programmers, one of the best ways that we can get noticed is to have our open-source projects available in as many places as we can find them. Github allows you to show your code to the world. The Octopress sidebar has built-in functionality of a list of your current Github repos, but wouldn’t it be cooler to have public activity feed that shows which of your repos have the most current commits?
Github provides an RSS key to all of your public activity. This format of your key will be:
Replace USERNAME with your username. For example, my public github key would be https://github.com/cjbrock.atom. If you subscribe to your own RSS feed, the key generated will be your private RSS feed. It looks something like this: https://github.com/username.private.actor.atom?token=somehextoken. Unless you would like to expose your private key to the rest of the world, please do not use this key.
There are many javascript PHP generators that allow you to copy and paste javascript right into your site, and will abstract away the complexity of writing your own RSS generator. FeedtoJS is the original cut and paste PHP generator. Luckily, it’s open source, and you can run your own instance of Feed2PHP if you’d like to. But for this, just insert your RSS key, and select your options that you would like. You can specify the number of items to display and showing and hiding item descriptions, as well as many other options. Feed2JS will generate code for you to copy and paste into your site.
A simpler version of Feed2JS is located here, but it is not guaranteed to stay live.
Feeds are best located in a column layout. The Students at Flatiron page uses a dual column layout with a Twitter feed, but you can use CSS or SCSS to format your output however you’d like.
1 2 |
|
This was a little intriguing, but not hard to fix. However, after trying to run gem install RedCloth, we got another error.
1 2 3 4 5 6 7 |
|
Since gem install had never failed for me when I ran it, I decided to do a little more research. What actually is RedCloth? Why do we need it?
RedCloth is a module for using the Textile markup language in Ruby. Even the official website said the gem install should work. The issue was not actually RedCloth, but it turns out after checking the mkmf.log it was an issue with gcc-4.2 also not being found.
gcc is the Gnu Compiler Collection. It includes support for multiple programming languages, as well as libraries for these languages. Even after making sure that command line tools were installed with Xcode, it still didn’t find the gcc compiler. It appears that the newest version of Xcode has removed gcc in favor of clang.
Luckily, she actually had gcc on her machine, but it wasn’t installed. We only needed to run the last two lines of code. If you don’t, run all five lines.
1 2 3 4 5 |
|
https://github.com/imathis/octopress/issues/59
http://jfire.io/blog/2012/03/02/xcode-4-dot-3-homebrew-and-ruby/
http://stackoverflow.com/questions/12119138/failed-to-build-gem-native-extension-when-install-redcloth-4-2-9-install-linux
http://jgarber.lighthouseapp.com/projects/13054/tickets/215-native-ext-compilation-failure
Enter Git and GitHub. Git is a version control system that helps you branch your code and bring it back together effectively, with a timeline that you can follow easily. GitHub is a web-based hosting system for projects that use Git. To date, it’s the most popular system online. But learning to use Git isn’t easy. These folks have put together some resources to help get you over that hump a little easier.
The amazing people at Code School and GitHub have collaborated to make the great GitHub simulator Try GitHub to get you familiar with the Git syntax. It runs you through basic commands like git init, git status, git add, git commit, and git push. After you’ve run through this, the syntax doesn’t feel so foreign anymore.
If you’ve run through Try GitHub and you still have questions, try the GitHub Help page. Chances are that if you have a question, many people before you have had the same one. The search function on the Help page is incredibly robust, and the answers help to walk you through the steps needed to perform whatever it is that is confusing to you. They also have helpful categories of articles, such as Bootcamp, Setup, Using Git, and Repositories among others.
If you’re still struggling a little bit, try Git - The Simple Guide. Available in 12 different languages as well as English, The Simple Guide walks hand in hand with you through all the steps to set up a GitHub repository with helpful links to downloading git in multiple operating systems. Illustrated with colorful graphics and playful fonts, this really is the simplest guide to getting started with GitHub, but it gives you a good brief overview of the functionality that Git offers.
For those of you who prefer a more visual guide, Railscasts offers a five minute screencast about getting started with Git. Railscasts are ubiquitous in the world of coding with over 350 episodes casted on almost every conceivable topic. This short and sweet guide to getting started with Git is Rails specific, but the concepts apply through all languages.
The GitHub Cheatsheet is a beautifully designed interactive site that allows you to click on the specific area, and it gives you a mouse-over list of the commands available as well as a short synopsis of the function. It also uses good graphic design to illustrate which commands cover multiple areas.
]]>Check out the video here:
The slide deck is here:
]]>Why do we test in Ruby? Ruby is a test-driven development language. Testing inherently shows you where your application should go next. It allows you to specify the behavior that you think should happen before you write code to enact that behavior. One of the most popular methods of testing in Ruby and Rails is RSpec.
RSpec is a behavior-driven development tool for Ruby programmers. Behavior-driven development is an approach to software development that combines test-driven development, domain driven design, and acceptance test-driven planning. RSpec includes a command line program, text descriptions of examples and groups, customized reporting, expectation language, and built-in mocking/stubbing framework.
As a beginner, I’m already thinking:
If you’ve completed the Ruby Koans, you’ve already been exposed to testing syntax. Testing in Ruby is done by a Red-Green-Refactor method. Basically, when you run your testing development tool, if the answer turns out red, your test fails. If it turns green, your test passes. And if it’s blue, you should check out your code for a way to refactor it and make it more efficient.
When you’re writing an application, feature creep is bound to happen. If you can manage to stick to a test-driven method of development, you can keep feature creep to a minimum. It would be nice to immediately scale your website for a billion users, but YAGNI: You ain’t gonna need it.
Testing is actually very simple. There are a few things to keep in mind, but overall if you trust your tests and allow them to guide your development you will not go wrong.
(Note: This blog post is a brief summarization of the testing techniques laid out by user Duien on Speakerdeck. Additional research and extrapolation is my own.)
]]>I hope to see Ruby help every programmer in the world to be productive, and to enjoy programming, and to be happy. That is the primary purpose of Ruby language.
Matz is a Japanese computer programmer and the creator of Ruby. He released Ruby in December of 1995 as a productive language that was still fun to use, empahsizing the feeling of the language rather than striving for language perfection. Ruby is said to follow the principle of least astonishment, meaning that the language should behave in such a way as to minimize confusion for experienced users (at least if the user is Matz). Now working as the chief architect of Heroku, he is currently developing a new open-source lightweight Ruby project called mruby.
The Ruby community lives by the mantra: “Matz is nice, so we are nice”.
When you don’t create things, you become defined by your tastes rather than ability. Your tastes only narrow & exclude people. So create.
Although he committed infocide on August 19, 2009, _why is still one of the most influential Ruby programmers to date. He is best known for Why’s Poignant Guide to Ruby, Hackety Hack, and the Try Ruby interactive programming tutorial. He has also contributed several libraries and applications to the coder community. His love of programming and language speak to even new programmers, as illustrated by Annie Lowrey’s March 2012 Slate Magazine article “Where’s _why?”.
Flexibility is not free. It’s overrated. And if you trade that flexibility in for some constraints, you get a lot of complexity removed from the equation, you get a lot of productivity back from all the stuff you don’t have to do.
Hansson is a Danish programmer who created Ruby on Rails, a web framework that is optimized for programmer productivity. It abstracts away a lot of code processes, leading some to criticize the “magic” of rails. However, Rails allows programmers a high level of productivity faster with a less painful learning curve than other languages. Hansson is also a professional race car driver.
There’s a joy to programming. That’s the reason why we’re here…to experience the act of falling in love with programming.
Avi is our fearless leader at the Flatiron School in New York City. A programmer since the internet was born, Avi brings a love of code and a passion for teaching to the school. Imminently quotable, Avi also organizes one of the largest Rails meetups in the world, NYC on Rails.
]]>Although the web page does offer you the option to run through the Koans online, it’s an abridged version and only offers 30 modules. However, if you choose to web-base your koans, be sure to bookmark where you left off, so you can pick it back up again later.
Even if you already have Ruby installed, walk through the Installing Ruby section of the webpage. it shows you how to get started with the Koans. To start the Koans, simply go to Terminal (or your choice of command line interface) and while in your Koans folder, type: The error message will point you directly to the file and line number. This will bring up your first test. You don’t really need to know about testing to be able to get through the Koans, you just need to understand that whenever you see a line like this: ___ you need to delete it and fill it in with something else.
If you know about the Koans, chances are that you have a basic Ruby knowledge base. Our first assert tells us exactly what to do, which makes guessing is pretty easy. Once the assert is correct, go back to terminal and run ruby path_to_enlightement.rb again, and your test should pass.
You can also test the code in IRB. This will (almost) always give you the correct answer. If you don’t know if nil is an object, run it in IRB.
If you’re really and truly stuck and can’t figure out the error message (usually about 15 minutes is a good rule of thumb when you’re having trouble) try Googling Ruby Koans and Github. Many, many, many people have completed and committed the Koans, and a lot of them have committed them. When in doubt, check out their code and try to write your own based off their solutions. Be sure to check out a couple different solutions - all solutions are definitely not created equal.
]]>