Articles tagged with 'ruby'
Ruby 1.9 has 4 different ways to deal with closures.
Cue music
Proc
Procs are the weird ones of the bunch. Technically, all of these things I’m going to describe are Procs. By that I mean, if you check the class, it’s a Proc.
A Proc is made by using Proc.new and passing a block, Proc.new { |x| x }, or by using the proc keyword, proc { |x| x }.
A return from inside exits completely out of the method enclosing the Proc.
A Proc doesn...
Stop me if you’ve heard this one before.
You get into some new tech over the weekend. With enough excitement to kill a horse, you whip up a ruby gem and put it on Github.
You get a few hits, some people using it over the next little while. A few bugs get reported, and you fix them. A few features get requested, and you implement them. A few pull requests get submitted too, and you merge them. You’re using it yourself, so you’...
I was bored last night, so I hacked this up. My original thought was since riak has an HTTP interface, I could just proxy GET requests to it when a short URL was used, but either I was doing it wrong or you can’t set the Location header when you POST documents. Oh well.
Anyway, this uses riak just to store the URL, and the riak key as the short URL key. There is no error checking, UI, or anything fancy. It’s pretty much the simples...
Here’s something I cooked up this evening. Nothing too epic, but it’s a neat illustration of metaprogramming with ruby.
Ruby allows you to reopen classes and add methods to them. You can also make a Module and include that in a class to add methods. I’ve added four methods to the Symbol class and overridden one method in the Array class (using alias to keep the old method around since super doesn’t quite work).
Now we c...
I was doing some optimizing the other day on a Rails 2.3.x application with a metal controller. I didn’t feel like writing a straight rack application, so I used sinatra. A pretty good explanation of how this works can be seen in Adam Wiggins’ slides, Rails Metal, Rack, and Sinatra
In Rails 3, they still have metal controllers, in that the ActionController::Metal class is essentially a really thin controller, and you can use it lik...
Do you have a TODO list? Sure you do. I bet you have all sorts of cool projects on it, like a better todo list application, a web spider for some RSS feeds, a search engine experimentation, a git backend built on riak, and a slew of other ideas you haven’t got around to.
I bet you also have some new programming languages you want to learn. Maybe expand your scripting language knowledge with some Python, break into functional programming ...
You may have heard, but Delicious is getting the boot.
I left Delicious long ago, because it never did anything for me. Yeah I could bookmark things, share them, whatever. I never went back to the bookmarks though. I wanted my bookmarks to work seamlessly with how I used the web. That means, it had to work with Google.
Back in the day
I used Google Bookmarks back when I was evaluating bookmarking apps in 2008 (when I settled on Delicious) and ...
If you’re in the ruby world, and specifically the web side of the ruby world (Rails, Sinatra, etc), you should probably know who Zed Shaw is. I mean, he only wrote mongrel, which you’re probably using as your application server.
Well, he’s been hard at work on Mongrel2, and it’s a big change from the original mongrel.
Mongrel2 isn’t a ruby web server. It’s (and I’m going to borrow straight from the sit...
It’s 10 minutes past 6 on Sunday evening, and we just wrapped up our Rails Rumble 2010 project.
Check it out at http://gemrage.com/. Check out our team here too.
This. Shit. Was. Real.
We had a rough start with lack of internet for 3 hours, had a few depressing times thinking we’d never finish, but we rocked our socks off with Windows and Internet Explorer, MacBooks, Twitter and Campfire, and produced a pretty cool application.
Ple...
It’s been a while, but we’re continuing the Top 25 Most Dangerous Software Errors with numbers 10-6.
10. Missing Encryption of Sensitive Data
Have you ever signed up for some website or service, only to receive an email 30 seconds later WITH YOUR PASSWORD IN IT! Doesn’t it just fucking kill you?
Have you ever used a ‘Forgot your password’ page only to be blindly given your password? Fantastic. I highly doubt they ...
Hello developers, how are you? Fantastic.
Does your code look like mine? No.
Could it load like mine? Yes.
Should you use rack-gist to load your gists? I don’t know…
Do you like not blocking your users while you page loads another Javascript file?
Do you want code that looks like it could write your app for you in the dream computer it built for you with its own DWORDS?
Of course you do.
Recursive Hasselhoff!
Into the best gisting...
Version 0.3.0 of sinatra-bundles is out and powering the blog for a week or so.
It’s cold out there, so bundle up:
% gem install sinatra-bundles
require 'sinatra/bundles'
Version 0.3.0 has a new feature and a slight API change.
Custom path prefixes
Yeah, I know, not everybody is as awesome as I am with their stylesheets and javascript files in the stylesheets and javascripts directories, but now you can control where they live on the dis...
It’s been a while, but I’ve been busy pwning n00bs at Modern Warfare 2 and Bad Company 2, and buying a car, so life has been pretty busy as of late.
Have no fear though! I continue the look at the Top 25 Most Dangerous Programming Errors with numbers 15 to 11.
15. Improper Check for Unusual or Exceptional Conditions
When you ASSUME things, you make an ASS out of U and ME.
This is all about assumptions. You assume something will w...
Rails developers aren’t exactly known for getting their indexes right (or even at all) on their databases. Granted, databases are a tough subject, and some people and companies make their living dealing with only databases, and some only with one database (like MySQL or Oracle).
If you’re coming to web development with no formal background in databases, and it’s all new to you, then it’s totally understandable to maybe ...
The Common Weakness Enumeration posted their Top 25 Most Dangerous Programming Errors last month. Most everything in the list is completely avoidable, but most new programmers, and especially those without real world experience (as opposed to trivial classroom projects), fall victim to at least some of them. A lot of them bit me in university and I still get nipped by some of them today.
Proper education is the first step, and the CWE have don...
A quick post for today.
Want to get rid of transactions from ActiveRecord for something? Here’s a cheap way to do it.
It only works for MySQL obviously, but you can roll your own if you are on postgres. I’ll make it a bit less crappy and make it a gem or something.
I pushed up a new version of sinatra-bundles and it’s running smoothly on my blog here, so I best tell you about it.
First, get your bundle on:
% gem install sinatra-bundles
require 'sinatra/bundles'
Version 0.2.0 has a couple fun things.
The sinatra dependency is bumped up to 1.0, since it’s out (yay!), it’s more awesome, and I’m not quite sure if the bug that prevented me from using earlier sinatra was fixed or not in...
I recently added a Javascript file to my bundle with sinatra-bundles that uses Javascript’s eval functionality. The call to eval executed code that referenced a method parameter. sinatra-bundles compresses Javascript files, and as part of that, shrinks variable names.
Normally this isn’t a problem, but in this case of eval, it became a problem, since it was trying to reference a local variable that no longer existed (because it got...
Lots of interesting things are afoot, and I try to keep track of them. Here’s what I’m watching.
Rails 3
Lots of new changes are coming with Rails 3, and it’s pretty exciting. I wrote about some of the cool ones here. Rails is shaping up to be faster, more modular, and generally better and easier to work with. With all the smart people working on it, you don’t have to look very hard to realize this is going to be a big ...
The Rails 3 Beta got dropped a few days ago, and the release notes for Rails were put out a bit before that.
The list of changes is long; this is a big release. There’s a lot to sift through, a lot to change to upgrade your existing application, and a lot to learn whether you are upgrading or starting a new app.
The big question is: WTF does all this mean to me?
Don’t worry, I’m going to tell you. If you want to read the full...
It occurred to me the other day, that I should take a look at the middleware I use on this blog. I don’t know what it was. My spidey senses just tingled.
Boy was I right. I totally had it backwards.
Rack middleware is a fantastic thing. It’s like a little encapsulated rack application that you can use to filter, process, or otherwise mess with responses. There is middleware to add etags, configure caching, catch and log exceptions,...
sinatra-bundles is an easy way to bundle CSS and Javascript assets in your sinatra application.
Yes! It has tests! They are on runcoderun
Usage
sinatra-bundles combines Javascript and CSS into one file. Meaning, you can bundle 2 or more Javascript files into one, similar with CSS stylesheets. Any bundled files are expected to be in the public directory, under ‘javascripts’ and ‘stylesheets’
Assuming you have the followi...
I used to have some really ugly code to generate the archive links you see on the right column of this blog. It was terrible. Granted it was some of the first code I wrote on this blog, and I was cruising through it because I just wanted to get it done and working, but still, it’s not really a valid excuse. It can be seen here.
I had a thought the other day that it would be much better handled by the Range class, and boy was I right, alt...
The folks at Thoughtbot have a Ruby community survey online, and you should all go take it. Well, as long as you are a ruby developer. Otherwise it doesn’t really matter.
Anyway, I wanted to throw out what I do for ruby style (and general programming style in some cases).
I hate the 80 character limit. Get a real monitor would you please?
I don’t align assignment operators.
I don’t indent private/protected keywords.
I ke...
In case you missed it, the Coding Horror, Stack Overflow and Haacked blogs died in almost a literal fire. Well I don’t think there was an actual fire, but basically they all hosted their blogs in virtual machines on the same physical server, and the backup process never backed up the virtual machine files since they were always in use. The backup process was managed by the hosting company and silently failed on these files. Oops.
While I...
Yehuda Katz’s slides from SD.rb on Rails 3. I can’t wait!
Vaporware To Awesome
I wish I could see this guy speak. I learn so much just reading his slides and other writings, I can’t even imagine what I’d learn if I was actually there.
Debugging Ruby
Scripting languages are the bomb. I pretty much live in them right now.
Ruby is my current (and probably will be for a long time) favourite scripting language, and it has a library for everything. Even if there isn’t a library for what you want, it’s super easy to write your own.
I ran into a problem a while back that ruby solved nicely for me. I was downloading wallpapers from a website, but it was getting annoying. Really, I just...
Hopefully you didn’t notice much, except the speed increase, but my blog is no longer on the behemoth that is Wordpress.
While Wordpress served me well for quite some time on my other blog it always kind of bugged me.
Well no more! I took a couple days (basically 2 full days and 2 evenings) and wrote my own little blogging engine from scratch. You can find it on github and get forking if you so choose.
What I used:
ruby and sinatra
They ...
@tmm1 and @joedamato put up a good slide show on slideshare about ruby threading issues, and their fixes. These things have been around for a while, but now they are here in slideshow form. Definitely check them out, even if you don’t program ruby, as there are some interesting profiling/debugging shenanigans shown, among other things.
Threaded Awesome
So check this: you have a nice little Rails app. You have some stuff for a JSON API type system. You’ve got an STI setup with the Widget model class and the more concrete FancyWidget and WeirdWidget classes. You’ve got a WidgetsController, and the corresponding FancyWidgetsController and WeirdWidgetsController
Yay. It works. You can deal with both types of Widgets using JSON.
Now you need an HTML interface to it.
Fine, you can just...
I looked at a few role systems for Rails, but never found what I wanted. They were all object based systems, never just “allow a user with this role to do this action”. Either that or I never found the systems that did that, or totally missed the docs on how to configure those systems to do what I wanted. Well, actually acl9 seemed to do that, but whatever.
So I made role_on
The instructions are on Github, so I won’t repeat t...
Ruby on Rails content_for
Project: Builder
RSS
pauldix-feedzirra
Basic Rails association cardinality
Nested Object Form with has_one Relation
I’m writing up some scripts to automate some EC2 setup, and SSH is required. All the examples for Net::SSH show using just a username and password, which is all good, but the Amazon stuff requires a key file. Here’s how to do it:
Net::SSH.start('my.amazon.hostname.amazonaws.com', 'user', :keys => '/path/to/keypair.pem') { |ssh| ... }
According to the docs, the :keys named param takes
an array of file names of private keys to us...
I like frameworks, but sometimes debugging them is more entertaining. I don’t know why I didn’t think of this method before, but mischa on github has a great little repo showing how you can do it.
The README
Usage:
Add:
require ‘ruby-debug’
require ‘cucumber_rails_debug/steps’
To features/support/env.rb
Then use:
Then debug # opens the debugger
or
Then what #prints out params, url and html
Check it ...
Selenium is a suite of tools to automate web app testing across many platforms. Cucumber is a BDD framework that allows you to write things in English (or whatever language you want, really), and have that execute as code. Put those together with webrat and rspec and you have a pretty mean stack to test your ruby on rails web application with. Sort of.
I’ve been having some problems with it, getting it set up, but it’s coming along...
Regex are always fun, and since regex expressions are first class citizens in ruby, it makes it so much easier.
I found this Code Golf question on Stackoverflow and threw together a quick method to do it. I can’t take all the credit, since I needed to search a bit to figure out if I could do matching parenthesis in ruby, and found that I could, but only in ruby 1.9, which ruby forum topic showed me. Then a commenter on SO pointed out ano...
In one of my classes, we used Ruby and Gtk, but some issues popped up. The most obvious is using a block to do GUI update stuff and the like, from another thread. Things die. Puppies are killed.
I found this post on Ruby Forum which fixed the problem.
Relevant code.
Basically, you call the Gtk.init_thread_protect method first when you start things up, then, whenever you need to do GUI update stuff, just wrap it in a Gtk.thread_protect {} block...
In case you just crawled out from under your rock just to read my blog, and haven’t the foggiest, cucumber is a ruby acceptance testing framework, and works great with rails.
I was writing features for admin users in this application, and needed a step like this:
Then I should be editing "test@example.com"
Where the quoted email is the login. Later on (in fact right after…I was testing edit functionality, and after safe you end up ...
The ruby-openid gem was giving me problems with Passenger, causing IOError problems.
I found the fix here
The gist of it?
Add
OpenID::Util.logger = RAILS_DEFAULT_LOGGER
To environment.rb, and you’re good!
My, what a nice rack you have. Rack is what Sinatra runs on, and it’s just a great interface for webservers.
Here’s some brain food.
32 Rack Resources to Get You Started
Sometimes, checking RSS feeds is boring.
Sometimes you just want to grab a picture or whatever from each item.
For that, there’s ruby. Oh and feedzirra.
There is an RSS feed I read, except it’s more of a picture feed and I just want to grab the pictures from it, and doing this through Google Reader is no fun.
That’s where ruby and feedzirra come in. feedzirra is a ruby gem for dealing with RSS feeds. It’s got a great in...
If you go download the ruby-gnome stuff, and GTK fails to compile, it’s easy to fix.
It whines about some defines, and you can just comment out the block that’s causing the problem.
I almost threw my laptop out the window figuring this out, but if you want to install ruby 1.9.1 alongside ruby 1.8.7 (whether the prefix is the same or not), you have to give ruby 1.9 a program suffix, but also a baseruby.
So you need something like this:
./configure --prefix=$HOME/local --program-suffix=1.9 --with-baseruby=ruby --enable-pthread
Oh me, oh my. When I first posted about this, in June of 2007, my original post started with
Ruby is sweet. I should use it more often.
And now, on Monday, I get to start my new job playing with Rails all day long. Funny how things change. Regardless, the quality of this tutorial hasn’t changed, so here it is again.
Rails Envy: Ruby on Rails Rake Tutorial