Jun
23
Simple way to “page” an Array in Ruby
Filed Under Software, ruby, technology | Leave a Comment
Unless you have ultra simplistic needs, (and are using Rails) I always recommend using the fantastic will_paginate plugin for paginating ActiveRecord, and even non-ActiveRecord collections! But, if you have a basic need to to page an array, you could open up the Array class and add this method:
class Array def page(pg, offset = 10) self[((pg-1)*offset)..((pg*offset)-1)] end end
This is a beautiful little method that adds #page to all arrays, and can be utilized like this:
my_array = [1,2,3,4,5, ... ,20,21,22,23,24,25] my_array.page(1) #=> [1,2,3,4,5,6,7,8,9,10] OR my_array.page(1, 5) #=> [1,2,3,4,5]
Feel free to use this if you like it!
Jun
23
Haml & Sass Bundles for TextMate
Filed Under Software, creativity, recommendations, ruby, technology | Leave a Comment
At Caring.com, we write our view templates using Haml and Sass (with Chris Eppstein’s Compass CSS Framework). It was a little strange to get used to at first, especially coming from tag-based/ERB-style frontend templating, but I have found that it is surprisingly straightforward, and succinct. TextMate, which is a fabulous text editor for rails and ruby, has two bundles that I use constantly that make my life easier (code coloring especially) in both Haml and Sass.
I am writing this blog post to share the bundles I use everyday with you, and so that I can remember where they live, for myself.
For Sass integration I use this bundle by seaofclouds:
git clone git://github.com/seaofclouds/sass-textmate-bundle.git "Ruby Saas.tmbundle"
For Haml integration, I use the “official” one provided by MacroMates (the creators of TextMate):
svn co "http://macromates.com/svn/Bundles/trunk/Bundles/Ruby Haml.tmbundle"
Clearly, you need to have both Subversion and Git installed to install these plugins, however you may be able to travel to github and download a zip file if need be.
Jun
18
PageSlide Plugin for jQuery
Filed Under Software, creativity, javascript, technology | 2 Comments
I’ve been working with Mr. Scott Robbins on his fantastic jQuery PageSlide plugin for the last few weeks. It has been a lot of fun, we’ve moved the project off of google code completely, and today have released version 1.1 of PageSlide on Github. version 1.1 is essentially a complete re-write touting a ton of new/awesome features, here is a list from the README:
- Choose slide direction (left or right)
- Custom width slides
- Modal slides
- unobtrusive
- Mutiple slide definitions on the same page
- Fully supported with IE7+, FireFox 2+, Safari 2+
- Minified version included!
I am very excited about this plugin, and will be using it in some of my projects very shortly! If you like the project, please “watch it” on Github and tweet about it and stuff while you’re at it :)
Get jQuery PageSlide here, it works with jQuery 1.26 and up!
EDIT: Live demo can be found here: http://derekperez.com/jquery-pageslide/demo/
Feb
9
Fibonacci
Filed Under Software, ruby, technology | 1 Comment
Thanks so much everyone for submitting your Fibonacci examples! One of the reasons why I asked everyone to do this is because it gives me (and all of us) a chance to learn about different approaches and different languages. So, in case you haven’t figured it out, it was sort of a trick question.
Fibonacci is generally very fast, unless you are over-architecting the solution. Let me show you what I mean:
def fibo(n) (!(n>2)) ? n : fibo(n-1)+fibo(n-2) end 37.times do |i| puts "n=#{i} => #{fibo(i)}" end
This was my first attempt. Its a beautifully complex execution of the algorithm, using recursion. However, notice that I am not storing any of the values that I am calculating? That causes the processor to go crazy around time 30. This would be what I call a classic example of over-architecting a simple problem.
But its cool right? :)
Here is my faster, smarter, more performant version:
$fibs = []; def fibo_calc(n) (!(n>2)) ? n : fibonacci(n-1)+fibonacci(n-2) end def fibonacci(n) if $fibs[n] return $fibs[n] else x = fibo_calc(n) $fibs[n] = x return x end end 37.times do |i| puts "n=#{i} => #{fibonacci(i)}" end
This is much much much faster, and can hit 30,000 iterations without missing a beat. The cooler thing too is that I can mix up the 30,000 integers that its calculating for, and it can run a “shortcut” using the array. Very cool.
So that was my nerd realization of the month. Of course, there are a billion ways to skin an algorithm, but sometimes the most concise or technically interesting way, is the slowest.
Feb
3
Nerd Challenge: Fibonacci Fun!
Filed Under Software, technology | 17 Comments
Lately, I’ve been studying algorithms, just for fun, because I am hardcore like that. Also, I’ve been very excited to test the speed/performance of the newly released and stable Ruby 1.9.1. I have been waiting patiently for the release, it is full of great new things like a modern VM, massive integer performance tweaks, language readability improvements, and full unicode support.
So I decided to do some masturbatory benchmarking, using Fibonacci! Also, did you know that Ruby has a builtin benchmarking framework?
Fibonacci is not a very difficult algorithm, though I learned a lot about what to do (and not do) when it comes to writing this sort of code in Ruby. (and any language for that matter)
So here is the challenge, write your best Fibonacci Sequence algorithm in any language you choose, discuss the pros/cons of your implementation, and speed of execution. Also, its mandatory that your algorithm calculates a minimum of 36 sequences deep, extra points if you get to 30,000 in less than 5 seconds.
In a few days, I will post how I did it in Ruby. I don’t really have a prize lined up or anything, maybe I will come up with something. At the very least, you’ll have bragging rights.
Jan
8
Why Twitter Makes FriendFeed Obsolete
Filed Under apple, business, life, recommendations, ruby, technology | 3 Comments
Anyone else out there tired of Twitter-spam? Every new web 2.0+ application that hits the web today asks you for your twitter credentials so they can post a 140 character spam about some various task you are performing at some website I don’t care about. However, there are times when I do care about those other various tasks my friends are performing on various websites, and that’s where FriendFeed comes in.
But I ask you, WHY BOTHER? Why am I going over to FriendFeed when I have a perfectly good Twitter timeline, full of friends I already know and trust, and chock-full of useless and semi-interesting pieces of information that I can’t seem to live without. Why are we duplicating all that information, when we could combine it into a master timeline (using Twitter), through a much more secure medium?
The as it stands today, If you want to broadcast your activities on a specific site through Twitter, you have to be ready to hand them over your username and password, that way they can essentially pretend to be you and make a post on your behalf. For some reason, that doesn’t seem to phase most people, but the ramifications of that act are sort of dangerous. As you may have heard, Twitter and the people who use it control many different spheres of influence, and that information falling into the wrong (and insecure) hands could be disastrous.
This is 2009! We’ve solved these sorts of problems in the past, lets put our thinking caps on! API, bitches. Twitter needs to introduce an API so that third-party vendors can post to your Twitter account without you having to fork over any nuclear secrets. Flickr has figured out how to do this very well…eons ago
“But Derek! Didn’t you start this post off by saying you hate Twitter-spam?”
Yes, I do. Thats why the next thing we need Twitter to do is setup “Twitter Feeds”, which will be where all of the twitter-spam is sent and categorized for when that information is sought after. Don’t think anyone will go out of their way to see that information? Friendfeed.com and its 5 million dollars in funding beg to differ. Twitter is the most natural place for this information to exist!
Why bother with FriendFeed at all when your “Twitter Feed” can say the last netflix movie you’ve watched, or that digg article about “the top 10 cats laying on their backs” that you thought was so awesome. This information, once officially supported by Twitter, can then be properly segmented and keeps my timeline about what it should be, you know, pictures of my cat and how many times I hate internet explorer.
This idea, however, is going to seriously impact the current load that Twitter is used to, even at 1.2 million new users a month (I read that somewhere, could be wrong). FriendFeed works through RSS, so the responsibility of that information’s existence is placed on the RSS provider, and not FriendFeed. So I could imagine their would be a substantial increase in data that is sent to Twitter, but having said that, they seem to be doing pretty good at handling themselves these days, and that would only make Google want to swallow them whole faster.
So basically, Twitter needs a ping API, and the pings sent by the API (that are theoretically web 2.0 spam) need to be segmented into their own grouping, that way we can do away with social networking social networks and utilize a popular technology we already love.
Dec
29
Why Does Apple Stifle Mobile Safari’s True Potential?
Filed Under Software, apple, technology | 6 Comments
The iPhone is hot. Its a badass micro-computer-internet-having-app-installing-jizz-casting-awesome-machine that can also make phone calls, but I have to bitch about one major flaw in the entire thing, and that flaw is Mobile Safari. Don’t get me wrong, I am a big fan of the mobile browser, it is leaps and bounds greater than anything else out on the market, but I ask you Apple, why not make it the greatest mobile browser on the market?
So what am I bitching about specifically? The iPhone has immense data harvesting capabilities, from geo-location, to the accelorometer, etc. But all of these fantastic API’s are available only to native, objective-c based applications, not Mobile Safari!? Explain this to my Apple, why is this not exposed through a JavaScript API? That is absolutely trivial to provide to the lowly web developer!
Apple and the iPhone are doing amazing things with the types of datasets it makes available to developers in the native context, all I am saying is, make it available to every context of applications! Libraries like PhoneGap are attempting to make this a reality, but again, the biggest limitation is the fact that PhoneGap still requires you to create a native application to act as a wrapper to your web application, this seems like a stop gap solution (heh) or an unfortunate hack.
Its also very apparent that Mozilla sees the future of location-based services being able to provide very personalized and exciting experiences to the web through their efforts in Mozilla Geode. Perhaps its true that at this point, that sort of data availability in a web browser could be seen as negative, but I am not sure I agree. I am willing to bet that within the next 6 years, Apple will be introducing GPS into their laptops, providing application developers with the same location-awareness capabilities on their computers that exist today on their phones.
Hopefully Apple will wise up in the future, but I highly doubt it. Something tells me this is a designed limitation, they are probably doing their best to keep control over what people are doing with that data through the app store vetting process. Oh well, one can dream!
Dec
24
Merb and Rails Come Together for Rails 3
Filed Under Software, ruby, technology | 2 Comments
I am absolutely excited about this news!! Yesterday, both the Merb and Rails camp have announced the news, so I won’t bother reiterating whats already been said. I’d rather talk about some of the major points that really get me excited about this announcement and how it is going to impact not only the history of Ruby on the web, but potentially web programming from this point on.
Fully Modular Core
I am happy that DHH and co have embraced the modularity concept that is one of the core ideals of the Merb project. Now, if you don’t want to Prototype as your JavaScript system, you can toss it and stick in jQuery, which will run 100% as well as the default. Although its 100% modular, the “Rails Way” is going to give you a smart set of defaults that you can completely customize to fit your needs!
I must admit, this is one of the sexiest things about Merb.
Speed is Quickly Becoming a Non-Issue
In the Ruby world, there is a ton of exciting innovation going on around building some amazing virtual machine technologies and really pushing the limits of what can be done with such an expressive and dynamic language. Also, we are seeing some fantastic developments on the deployment side of things that are making it not only extremely easy to deploy, but faster the serve requests as well.
So naturally, the next place to go for optimization is our framework. Merb has already made a ton of strides in making a thread-safe, and extremely fast framework. This development stack is going to scream in 2009!
AmazingĀ Base and Synergy
Between the Merb and Rails technologies and groups, there is going to be all kinds of new things to learn and all sorts of great people to work with and learn from. I am absolutely excited about what all of this means for the Ruby/Rails/Merb communities. We have got one of the fastest growing and fully-integrated technology stacks available for the internet today as an open source foundation.
With the emerging ecosystem of technology and thought that is occurring in this community, we are going to be seeing some amazing things in 2009.
Dec
11
Changing The World, In Trivial Ways.
Filed Under Software, life, recommendations, technology | 2 Comments
Lately I’ve been doing a lot of thinking, thinking about how technology and the internet have been applied in such fascinating and unbelievable ways, ways that have absolutely re-written the fabric of how we as citizens of earth interact with information, but also each other. When I think about all of this, the truly astounding thing is the passage of time that has elapsed. Companies like Apple, Google, and Wikipedia are doing their best to make sure that the way we interact today, is nothing like the way we will interact tomorrow.
These are visionary companies, ran by some of the best and brightest of our time, can you imagine a world without Google? (your children won’t be able to). The internet itself has been the biggest catalyst of our 21st century evolution, providing a platform and foundation for limitless connectivity, where our ideas and capabilities are the only limitation.
Its not often that I feel so inclined to be nostalgic about an industry that hasn’t even been around for 50 years, but lately, I’ve been evaluating my place in the scheme of things. I’ve been watching the innovations and “innovations” that have hit the market in 2008, and frankly, I think we can do better.
But this isn’t new, people have been inventing solutions to problems nobody has had forever. I guess you could say, this is my letter to myself to make sure I am actually doing things and working with people who want to make a difference in the world, rather than “minnovate”. People say that times like this (referring to our economy) are when people sit down and do the most amazing things, not because its the hip thing to do, but moreso out of necessity.
So here is to a world changing 2009.
Oct
28
Linkedin Announces Business Application Platform
Filed Under Software, business, technology | Leave a Comment
Linkedin just minutes ago announced they’ve thrown their hat in the “social application platform” ring! This is exciting news for people who are poised to bring innovative and interesting applications to the business and professional network.
Read more about the press release here at TechCrunch. As of today, there is no documentation or word on how exactly one creates an application for the new platform, but you can contact them to recieve more information!
keep looking »