So it turned out that, though we were supposed to finish at 11, almost everyone was still working on it through lunch. We kept at it for a bit, and then Avi took those who wanted to watch him implement a solution and ran through it. Others kept plugging away.
At the end of all this, it was 4PM, and the day was nearly over.
OK, OK, I know you want to hear about how I did. I finished at noon, and then spent the next few hours refactoring and making things prettier, OK? Sorry, didn't want to brag, but you forced me to.
Actually, I had a lot of fun with it - instead of defining arrays with 3 elements (x-coordinate, y-coordinate, reference to another point), I created a Node class based on a Struct with elements
x
, y
, and reference
.Huh? you ask. What's a Struct? Well, I'm glad you asked. We did blogs in the late afternoon, and today I presented. You can see my post at http://amcaplan.wordpress.com/2014/03/05/struct-rubys-quickie-class/. Structs are awesome little class factories that create simple data storage classes and give you lots of extra methods to go with.
In lecture, we discussed the creation of a nested
params
hash in Sinatra to create objects from forms in a more organized way. A properly constructed params
hash makes construction of objects really simple. Let's say I have a Person
object with name
, height
, and weight
attributes. I could do this:
person = Person.new
person.name = params[:name]
person.height = params[:height]
person.weight = params[:weight]
person.save
But I can also do it more simply if I have everything nested inside params[:person]:
person = Person.create(params[:person])
Isn't that much nicer?
We were already overloaded with homework from yesterday, but we got some more today anyway, for whenever we finish that. I just did as much as I could from yesterday's homework.
Skills developed: Breadth-first search, use of nested params for forms in Sinatra
No comments:
Post a Comment