A few weeks back I decided to bite the bullet and get on the Rails Train. Complete confusion was my first thought upon reading through a few Rails tutorials. There is no end to the number of articles about Rails on the web (now there is one more!) and I was feeling a little lost trying to determine where to start. So I figured I'd start with the basics and began reading about Ruby (created by Matz in Japan), the base language of Ruby on Rails.
I purchased Ruby by Oreilly and read through it diligently, computer always at the ready to test out examples from the tome. Ruby takes a little while to get your head around, especially if you are a closet, wannabe coder n00b like myself. For example, I was used to iterative loops such as (for PHP):
for($i=0; $i > myCollectionOfThings.len(); $i++) { prints myCollectionOfThings[$i] }
What the heck are blocks? Yield? Bah! Needless to say, it took a few days to get used to.
So now with just enough Ruby under my belt to make me dangerous, I moved on to Rails. I started with the basics again, reading through each section of the Ruby on Rails website. I was thrust into a land of migrations, YAML, DRYing, REST, MVC (model, view, controller), routing, and other magical concepts.
As I pushed forward from topic to topic, making progress here, hitting a wall there, it started to make sense why the Rails logo-of-choice is a train. I started to feel like a coding conductor, all my objects easily aboard my locomotive, quickly eating up the track and rushing towards project creation (wow, that was possibly the nerdiest thing I've said in a long, long while). I remember being amazing, even feeling a little sorry for myself for not getting aboard sooner! I started to see projects written in other languages would take immensely longer to develop, and worse, would not be as elegant, well organized, and easily shared. The MVC framework and "Convention Over Configuration" principle guide Rails developers, from the newbies to the veterans, on how a basic web-app is structured, how to use as little code as is efficiently and "readibly" possible, and what goes where.
The train metaphor was also driven home recently when I was trying to deploy an app on a remote server. I felt like I was in the middle of a train wreck. As wonderful and beautiful as Rails is, it is also a complicated organism with a lot of deep, moving parts. Rolling out an application has even more layers to it and computers don't really do "hey, you *almost* got the configuration right", but rather just crash.
But after spending about and hour or two tracking down my issue and debugging (more to come on this via another post), I pulled myself from the twisted metal and wreckage, relieved and happy again. Rails does a great job of telling you where it is getting tripped up so you can Sherlock Holms your way out of it (the issue ended up being an outdated gem the hosting service was forcing me to unknowingly use, rather than my nice, shiny, up-to-date ones). Using a different technology likely would have taken me several times longer.
Gems bring another element to Rails that really should make anyone thinking about getting into Rails jump on in. The iPhone is the beautiful gadget, but the apps put it head and shoulders above the rest. Same with Rails. There is a vibrant, talented, and witty group of developers that contribute amazing add-ons to a Rails application ("There's a gem for that" anyone?).
It really amazes me how abstracted web development has become. A year or two ago I was writing straight HTML, crafting SQL statements, writing login scripts, and the like. Now its all HAML, find_by_this's, acts_as_auth, etc. While I initially felt a bit disconnected by this, the benefits it brings are immense: less time worrying about stuff that has been done and tested a thousand times leaves more time to build a better service and be a better developer.
You Should Check This Out
Showing posts with label nerd alert. Show all posts
Showing posts with label nerd alert. Show all posts
Tuesday, January 5, 2010
Thursday, November 19, 2009
Augmented Reality. Better Than Real Reality (well, not yet!)
Hello all, hope your day is going well. Mine? Thanks for asking. It is great. So one thing that has been on my mind for sometime is Augmented Reality.
For those that aren't familiar with this, I really like how Robin Wauters at Techcrunch describes it:
I like to think of it as one (small) step closer to being a total virtual reality setting, however you are still based in real reality (i.e. walking around in the real world, but seeing virtual things combined with real world things).
There are several companies and applications that are already making some waves in this space. Layar is the one that comes to my mind. Another is the Urbanspoon iPhone application. With it, you hold up your iPhone and it will overlay aggregated restaurant approval rating:

The main short-comings in the fledgling augmented reality industry:
This delivery method is silly. I do not want to go around holding up my phone and viewing it through a little screen. Worse, you are not immersed in your new, layered world. You are only getting an extremely small sneak peak of this altered reality. What would be better?
What if you had a set of glasses that you could plug into your phone that could send / receive information and lightly overlay the rendering data to the glasses? Much like a "Heads Up Display" used in fighter jets and some car windshields. You would now have a hands free device that displays the request information in a much more elegant manner and you can experience the augmented reality more seamlessly.
Under this set up, the phone is the data receiver / sender, the application on the mobile device is the processor, the application it is sending / receiving data to / from is the data source, and the glasses are the display. Creating such a product would be quite expensive and take excessive R&D, so without a large market to pull down revenue from, there is little incentive to venture into it (too much perceived risk, not enough perceived reward). This brings me to my second point.
All the applications I've seen that augment reality are cool, but that's about as much praise as I'd give them. There is no really, really compelling application that would significantly improve my life and make me want to part with my hard earned dollars. Showing me who and where people are around me that recently submitted Tweets? Displaying a pin-point on a restaurant I'm looking at and showing me a review? Booooo.
I don't know what the game changing application would be (if I had an idea, I wouldn't be writing this post, I'd be trying to build it!), but fine, I'll take a guess. I think it could be a game. Yes, a game.
Imagine it! People go out and play paintball and laser tag. I pay $50 per year to play Halo 3 against other people from around the world. What if you could be playing a real life game in a large structure or enclosed area, but instead of shooting your friends with balls of paint or firing "lasers" at them you are fighting against "virtual" enemies, or aliens, or whatever? What if you could be in a field with a group of friends in real life. All of you are wearing the aforementioned glasses for visual augmented reality, a head set for communication with your team and, more importantly, for *audio* augmented reality (which would be in step with the visual augmented reality), and with a "gun" that is also synched with the rest of the whole system. All of a sudden a space ship screams in from above, extra-terrestrial guns blazing? And then zombies come sprinting at you from the tree line? You and your friends must act as a team and defend your area and fend them off!
I'd pay for that.
For those that aren't familiar with this, I really like how Robin Wauters at Techcrunch describes it:
... basically it’s the placement of a digital layer of information on top of a real-life view of the world around you, as seen through e.g. a mobile phone’s camera lens. Using augmented reality, you could be using your smartphone to glance around the main square of a city you’re visiting and get up-to-date information about nearby restaurants, ATMs, real estate offers, and more on-screen, bolted on top of what you’d be seeing if you weren’t looking through the lens.
I like to think of it as one (small) step closer to being a total virtual reality setting, however you are still based in real reality (i.e. walking around in the real world, but seeing virtual things combined with real world things).
There are several companies and applications that are already making some waves in this space. Layar is the one that comes to my mind. Another is the Urbanspoon iPhone application. With it, you hold up your iPhone and it will overlay aggregated restaurant approval rating:

The main short-comings in the fledgling augmented reality industry:
- Crappy viewing methods
- No killer-app yet
This delivery method is silly. I do not want to go around holding up my phone and viewing it through a little screen. Worse, you are not immersed in your new, layered world. You are only getting an extremely small sneak peak of this altered reality. What would be better?
What if you had a set of glasses that you could plug into your phone that could send / receive information and lightly overlay the rendering data to the glasses? Much like a "Heads Up Display" used in fighter jets and some car windshields. You would now have a hands free device that displays the request information in a much more elegant manner and you can experience the augmented reality more seamlessly.
Under this set up, the phone is the data receiver / sender, the application on the mobile device is the processor, the application it is sending / receiving data to / from is the data source, and the glasses are the display. Creating such a product would be quite expensive and take excessive R&D, so without a large market to pull down revenue from, there is little incentive to venture into it (too much perceived risk, not enough perceived reward). This brings me to my second point.
All the applications I've seen that augment reality are cool, but that's about as much praise as I'd give them. There is no really, really compelling application that would significantly improve my life and make me want to part with my hard earned dollars. Showing me who and where people are around me that recently submitted Tweets? Displaying a pin-point on a restaurant I'm looking at and showing me a review? Booooo.
I don't know what the game changing application would be (if I had an idea, I wouldn't be writing this post, I'd be trying to build it!), but fine, I'll take a guess. I think it could be a game. Yes, a game.
Imagine it! People go out and play paintball and laser tag. I pay $50 per year to play Halo 3 against other people from around the world. What if you could be playing a real life game in a large structure or enclosed area, but instead of shooting your friends with balls of paint or firing "lasers" at them you are fighting against "virtual" enemies, or aliens, or whatever? What if you could be in a field with a group of friends in real life. All of you are wearing the aforementioned glasses for visual augmented reality, a head set for communication with your team and, more importantly, for *audio* augmented reality (which would be in step with the visual augmented reality), and with a "gun" that is also synched with the rest of the whole system. All of a sudden a space ship screams in from above, extra-terrestrial guns blazing? And then zombies come sprinting at you from the tree line? You and your friends must act as a team and defend your area and fend them off!
I'd pay for that.
Labels:
nerd alert
Wednesday, August 5, 2009
Triangle Problem + Solution
Question: "If you break a straight line randomly in two places, what is the probability that you can form a triangle from the resulting three pieces?"
Please provide the proof + code for a simulation of 10,000 trails! Will post the solution tomorrow so get crackin!
------------------------------------------
Solution:
First off, congratulation are in order to a Mr. Paul R. and a Mr. Dan D. who both successfully utilized the higher power of numbers and reached the correct answer. A slow clap to you both. A special mention is in order for a Mr. Frank I. who uncovered about 80% of the solution, and to a Mr. Elliot G. who at least attempted the problem (percentage of success was not capable of being calculated for this last attempt).
The Proof. We first have to determine what, when given three different lines of varying lengths, is required to construct a Triangle? In order to create a triangle from three different lines of varying lengths, we need the following condition to hold:
Now, looking at the posed question, lets say that the stick’s length is L, and we have two random breaks, a and b. We can draw the following representation:

We can see that, in this case, a < b. We can also see that there are three lengths that result from this break, x, y, and z. We have the following equations now:
1. x + y + z = L
2. a < b
3. x = a
4. y = b – a
5. z = L – b
So we have to think about the value-space that a and b can take on, namely that 0 < a < L and 0 < b < L, or graphically:

This is a representation of all possible values a and b can take on. Now let’s assume that x >= y >= z so from the starting condition that must be fulfilled to make a triangle, we have:
x < y + z => a < (b – a) + (L – b) => a < -a + L = a < L/2
So in order for a triangle to be the result, a must be less than half of the entire length. Furthermore, because x = a and x is the largest, y and z must also be less than half of length L. We see that no single side can be greater than or equal to L/2 if we want to be able to make a triangle from the sides.
So we can update our equations:
1. x + y + z = L
2. a < b
3. x = a and x < L/2
4. y = b – a and y < L/2
5. z = L – b and z < L/2
So we can now update our set of values drawing – we know that a < b and a < L/2 from updated equation #3, so:

The values of that a and b can take on, when a < b, that will produce a triangle are now outlined in green. Next, from updated equation #5 we have that L – b < L / 2, which equals b > L / 2:

The values of that a and b can take on, when a < b, that will produce a triangle are now outlined in green. Finally from updated equation #4 we have that b – a < L / 2, which equals b < a + L / 2 (this is a linear condition – line has a slope of 1 and b intercept of L/2:

So the area that meets all of these conditions is our winner (in green):

Which we can see is equal to 1/8 (0.125) of the possible values.
However, remember that this assumes that a < b, and because we know that a and b are both random numbers, they are interchangeable, so we can also have the following situation:

So this scenario must be taken into account. We now assume a > b, which yields the following starting equations:
1. x + y + z = L
2. a > b
3. x = b and x < L/2
4. y = a – b and y < L/2
5. z = L – a and z < L/2
We are still working with the same initial work space for a and b, but we have a different limitation being applied, namely a > b.
If we rearrange all these new equations as we did above, we arrive at the following conditions:
Starting Condition: a > b
From #3: b < L / 2
From #4: b > a – L / 2
From #5: a > L / 2
Which we can plot graphically:

And the only area of intersection for all equations mirrors what we found before, reflected across the line b = a:

Finally, we combine both scenarios for a > b and b < a and the resulting areas that satisfy our starting stipulation (long < medium + short), and we get the following result set:

Which yields and answer of 1/8 + 1/8 = 1/4 or 0.25 or 25%. So if you break a straight line randomly in two places, the probability that you can form a triangle from the resulting three pieces is 25%.
The Code. The code to run the simulation. This is written in Ruby.
Running this simulation produces results that cluster around the expected value of 25%.
Please provide the proof + code for a simulation of 10,000 trails! Will post the solution tomorrow so get crackin!
------------------------------------------
Solution:
First off, congratulation are in order to a Mr. Paul R. and a Mr. Dan D. who both successfully utilized the higher power of numbers and reached the correct answer. A slow clap to you both. A special mention is in order for a Mr. Frank I. who uncovered about 80% of the solution, and to a Mr. Elliot G. who at least attempted the problem (percentage of success was not capable of being calculated for this last attempt).
The Proof. We first have to determine what, when given three different lines of varying lengths, is required to construct a Triangle? In order to create a triangle from three different lines of varying lengths, we need the following condition to hold:
Long side < short side + medium side
Now, looking at the posed question, lets say that the stick’s length is L, and we have two random breaks, a and b. We can draw the following representation:
We can see that, in this case, a < b. We can also see that there are three lengths that result from this break, x, y, and z. We have the following equations now:
1. x + y + z = L
2. a < b
3. x = a
4. y = b – a
5. z = L – b
So we have to think about the value-space that a and b can take on, namely that 0 < a < L and 0 < b < L, or graphically:
This is a representation of all possible values a and b can take on. Now let’s assume that x >= y >= z so from the starting condition that must be fulfilled to make a triangle, we have:
x < y + z => a < (b – a) + (L – b) => a < -a + L = a < L/2
So in order for a triangle to be the result, a must be less than half of the entire length. Furthermore, because x = a and x is the largest, y and z must also be less than half of length L. We see that no single side can be greater than or equal to L/2 if we want to be able to make a triangle from the sides.
So we can update our equations:
1. x + y + z = L
2. a < b
3. x = a and x < L/2
4. y = b – a and y < L/2
5. z = L – b and z < L/2
So we can now update our set of values drawing – we know that a < b and a < L/2 from updated equation #3, so:
The values of that a and b can take on, when a < b, that will produce a triangle are now outlined in green. Next, from updated equation #5 we have that L – b < L / 2, which equals b > L / 2:
The values of that a and b can take on, when a < b, that will produce a triangle are now outlined in green. Finally from updated equation #4 we have that b – a < L / 2, which equals b < a + L / 2 (this is a linear condition – line has a slope of 1 and b intercept of L/2:
So the area that meets all of these conditions is our winner (in green):
Which we can see is equal to 1/8 (0.125) of the possible values.
However, remember that this assumes that a < b, and because we know that a and b are both random numbers, they are interchangeable, so we can also have the following situation:
So this scenario must be taken into account. We now assume a > b, which yields the following starting equations:
1. x + y + z = L
2. a > b
3. x = b and x < L/2
4. y = a – b and y < L/2
5. z = L – a and z < L/2
We are still working with the same initial work space for a and b, but we have a different limitation being applied, namely a > b.
If we rearrange all these new equations as we did above, we arrive at the following conditions:
Starting Condition: a > b
From #3: b < L / 2
From #4: b > a – L / 2
From #5: a > L / 2
Which we can plot graphically:
And the only area of intersection for all equations mirrors what we found before, reflected across the line b = a:
Finally, we combine both scenarios for a > b and b < a and the resulting areas that satisfy our starting stipulation (long < medium + short), and we get the following result set:
Which yields and answer of 1/8 + 1/8 = 1/4 or 0.25 or 25%. So if you break a straight line randomly in two places, the probability that you can form a triangle from the resulting three pieces is 25%.
The Code. The code to run the simulation. This is written in Ruby.
#!/usr/bin/ruby
numOfTrials = 10000
numTriangles = 0
numOfTrials.times do
end
pct = numTriangles.to_f / numOfTrials * 100
puts "Number of Trials: " + numOfTrials.to_s
puts "Number of Triangles: " + numTriangles.to_s
puts "Percentage: %" + pct.to_s
numOfTrials = 10000
numTriangles = 0
numOfTrials.times do
# generate two random breaks, ordered
breaks = [ rand, rand ].sort
# load the length calculations via the breaks into an "lengths" array
# order the lenghts
lengths = [
breaks[0] ,
breaks[1] - breaks[0] ,
1 - breaks[1]
].sort
# add 1 to triangle var if we have a triangle
numTriangles += 1 if lengths[2] < lengths[0] + lengths[1]
breaks = [ rand, rand ].sort
# load the length calculations via the breaks into an "lengths" array
# order the lenghts
lengths = [
breaks[0] ,
breaks[1] - breaks[0] ,
1 - breaks[1]
].sort
# add 1 to triangle var if we have a triangle
numTriangles += 1 if lengths[2] < lengths[0] + lengths[1]
end
pct = numTriangles.to_f / numOfTrials * 100
puts "Number of Trials: " + numOfTrials.to_s
puts "Number of Triangles: " + numTriangles.to_s
puts "Percentage: %" + pct.to_s
Running this simulation produces results that cluster around the expected value of 25%.
Labels:
challenges,
code,
math,
nerd alert
Subscribe to:
Posts (Atom)