Friday, August 15, 2008

Juggernaut Push Server


Juggernaut is a Rails plugin that allows you to push information out to the client, without them having to specifically request it. For certain domains, a push server is an excellent solution. Juggernaut uses a small bit of Flash to open up a flashxmlsocket from the browser to the push server, and it subscribes to the service. My roommate Taelor has been playing around with it for a little while, and has put together a chat application that creates rooms off of Digg topics. Its called Shovel Chat. He has also made a few blog posts about Juggernaut and using it to create a chatroom with rails at his blog, and has posted the source code on GitHub(note that you will need to go through his blog posts and edit some of the gem's files...)


So, the way the chat app works is that when you request the show action for a chatroom, some flash gets sent to subscribe to the channel on Juggernaut. Juggernaut will then send you a list of all the people in the room. If you type in a message and hit enter, the message gets posted to your Rails app. Inside the controller, the message gets sent to the Juggernaut server, which then pushes the message out to anyone subscribe to that channel. You could achieve a similar effect using AJAX, but you would have to have each client polling the server for updates to the chatroom, but this would be really difficult to scale.


Anyway, I've gotten his source code installed on my machine and have begun playing around with it, and hope to start contributing to his chat_sandbox on github soon. Feel free to make your own fork of his code on github and start contributing, and stay tuned for more updates!

-Ralph

Tuesday, August 12, 2008

The Ruby Hoedown

Last Thursday, my roommate and I packed and headed 3 hours south to the Ruby Hoedown in Huntsville, AL. It was held at the Shelby Center at UAH on Friday and Saturday. This was my first programming conference, so I thought I might jot down a little bit about how it went.


The first presentation was by the guys at Rails Envy. They talked about innovation in Ruby over the past year. I'm a big fan of these guys' podcasts and tutorials, and they gave a great presentation.


Next up was Robert Dempsey, who talked about Cloud Computing with Rails. He made a great argument against all the folks who say that Rails can't scale. He gave us very detailed explanations of what a Cloud is, and what scalability is. He showed us what it takes to use Amazon Web Services to scale a site up, and then gave us a brief descriptions of different utilities out there for scaling with a cloud: Amazon Web Services, Heroku, Joyent, Accelerator, Morph. One particularly intresting platform was Vertebra, which is being developed by the folks at Engineyard.


Vertebra is a "Next Generation Cloud Computing/Automation Framework". Ezra Zygmuntowicz has some slides posted at his blog. The backbone of the system runs on horizontally scalable erlang XMPP servers. XMPP is an IM/chat protocol this is very efficient. Ezra posted this tidbit on his blog: "Just to answer the question about Vertebra being open source or not. Yes vertebra will be open source, the ruby framework, the protocols and security stuff will all be open source. We may go with a commercial license on the workflow engine as that is a large piece of engineering but we have not decided yet. We want to get this out there and see what people do with it as I think there are limitless possibilities here. But we need to lock down the protocol and document everything and we are still experimenting with different parts of the system. I'd hope to have something to release in 4-6 weeks." Yehuda Katz gave a talk about Vertebra on saturday, and I had a chance to chat with him for a few minutes. He mentioned that some of the folks from Heroku were helping out on the project.


Jim Weirich and Joe O'Brien gave a great talk about mocking. This was an interesting talk because they acted it all out like they were 2 developers working on different problems that involved using mocks. The first one involved using a mock object to log in during testing, and in another one, they showed how you can refactor code so that you will be able to use a mock with it. I chatted with these guys at lunch on Saturday for a while.


Rein Henrich gave a very entertaining talk on Ruby Best Practice Patterns. He started off with a comical presentation on code unfactoring. He showed us different methods of making your code so unreadable that you are the only person able to reasonably maintain it. "Job security through code obscurity." Some of the methods he showed us were: un-DRYing your code; naming methods and variables in pig latin; and taking code that is abstracted into several small functions, and dumping it all into one huge one. After the talk on code unfactoring, he went on into his real presentation on best practice patterns. One of my favorites was the execute around method.


Some other notable talks at the conference included: bryanl(blog smartic.us), who talked about why you should "Test all the fucking time"; the truthy gem; Obie Fernandez's great talk on running a successful company; keynote talks by Chris Wanstrath and David Black; Troy Davis did a presentation on Adhearsion, a ruby framework for making and recieving phone calls; Giles Bowkett used Ruby and his program Archaeopteryx, to create music with code; Being a guitar player, Giles presentation was very interesting to me..unfortunately, Archaeopteryx is only availible for the Mac right now.


I had a great time at the conference. It was cool to be around so many other Ruby enthusiasts, and everybody was really nice. I definitely plan on going next year. Jeremy mentioned that he is planning on holding the Hoedown at Opryland Hotel in Nashville(only an hour away!), and that he hopes to get enough sponsors to host it for free. Even if it isn't free next year, I will gladly pay out another $200 to go again. If you've never been to a conference before, I highly recommend it. It was a very gratifying experience.


Well, thats all for now, I know I promised a post about my Google Summer of Code project, but I don't have it all together yet. If you are interested, it is a c-extension to speed up some of Ruby's CGI functions. You can check out the repo on github here. Farewell until next time!



Edit: Also, you can get videos of all the talks at ConFreaks. I spent a while chatting with both of the guys there, and one of them let me plug in my laptop to their powerstrip for the majority of the conference(I need a new battery...).

Sunday, July 27, 2008

Been a little while...

Hi all, thought I would make a post and update things. I've been busy working on a couple of different gigs: My Place Media, and the Google Summer of Code. Anyway, My Place Media just ran out of money(lots of bad decisions by the guy calling the shots), so I've got a lot more time to concentrate on my GSoC project.


My GSoC project is to working on extending parts of Ruby's CGI class in C (mainly the query string parsing and multipart form parsing). My mentor for the project is Pratik Naik, a Rails Core member. I'll have some more info on how the project is coming along in another post.


Anyway, I've got some ideas for expanding RailsonEdge out a little. The first is that I've purchsed the domain name railsonedge.com, and I have a shared host server. What I would like to do is to use my server to provide hosting for a few open source Rails apps, and let the developers of them either post to the blog, or just put in a feed from their blog. So if there is anyone out there that would like free hosting for your Rails project, to show it to the world, shoot me an email at rledge21 -at- gmail -dot- com, and tell me a little about yourself and your project.


I'm planning on posting some new tutorials very soon (maybe even doing a screencast, but I've not decided whether my voice would scare everybody off or not), and putting up a code snippet repository (similar to snippets.dzone.com)


Anyway, that is what is going on. Expect some posts over the next couple weeks with a couple tutorials, as well as an update to my GSoC project...later all.

Friday, May 2, 2008

Using Symbols

If you all are anything like me, symbols confuse the hell out of you. You may use them, but you don't really understand them. I decided to research it out a little today and see what I could figure out. It turns out that there are a lot of different viewpoints of symbols out there. Here are some articles I came across:



The first is a detailed overview of symbols, the second shows some of the way Rails uses symbols.


So, as a quick summary, here is what I know about symbols:
A symbol is used to represent strings, without the overhead of actually creating a string. They don't have the functionality that you get with a string, and the value of them can't be changed, but they are much more efficient than using a string. Here is part of the example from the Gluttonous article:



> patient1 = { "ruby" => "red" }
> patient2 = { "ruby" => "programming" }
> patient1.each_key {|key| puts key.object_id.to_s}
211006
> patient2.each_key {|key| puts key.object_id.to_s}
203536

The key in both hashes creates(string "ruby") has a string object created each time. So, in this example, we have 2 seperate string objects with the same name. You can achieve the same result with symbols:



> patient1 = { :ruby => "red" }
> patient2 = { :ruby => "programming" }
> patient1.each_key {|key| puts key.object_id.to_s}
3918094
> patient2.each_key {|key| puts key.object_id.to_s}
3918094

The difference here is that the same symbol is referenced in both of these. Not only is symbol a smaller object, but it gets reused.


So I am curious, what ways do you all use symbols?



-Ralph

Thursday, May 1, 2008

Intro to the Rails Console

Hi all. Today I'm going to give you a bit of an introduction to the Rails console. Using it will make your life a lot easier. To open it up, type ./script/console from your project directory. From here you can play around, test out commands, add objects to the database if you haven't created an page to do it yet. I'll be using the console from my Forum tutorial to give you examples, so here we go!


First, let's load all of our topics from the database into a variable:



>> @topics = Topic.find(:all)

Which outputs the following:



=> [#<Topic id: 1, forum_id: 1, user_id: nil,
subject: "My Rails 2.0 Forum Tutorial",
body: "Check it out, http://railsonedge.blogspot.com",
created_at: "2008-03-06 11:11:07",
updated_at: "2008-03-06 11:11:07">,
#<Topic id: 2,
.......

Easy stuff. Now lets take that hash, and make it into something useful:



y @topics

The 'y' function outputs everything in yaml format, much more readable:



---
- !ruby/object:Topic
attributes:
updated_at: 2008-03-06 11:11:07
body: Check it out, http://railsonedge.blogspot.com
subject: My Rails 2.0 Forum Tutorial
id: "1"
forum_id: "1"
user_id:
created_at: 2008-03-06 11:11:07
attributes_cache: {}

- !ruby/object:Topic
attributes:
updated_at: 2008-03-06 11:11:37
body: Check it out! http://railsonedge.blogspot.com
subject: New Blog Post Today!!
id: "2"
forum_id: "1"
user_id:
created_at: 2008-03-06 11:11:37
attributes_cache: {}

- !ruby/object:Topic
attributes:
updated_at: 2008-03-09 00:35:44
body: Welcome to the new forum.
subject: Welcome!
id: "3"
forum_id: "2"
user_id:
created_at: 2008-03-09 00:35:44
attributes_cache: {}

- !ruby/object:Topic
attributes:
updated_at: 2008-03-09 00:36:04
body: What time is it?
subject: "Question:"
id: "4"
forum_id: "2"
user_id:
created_at: 2008-03-09 00:36:04
attributes_cache: {}

Now, lets grab just one of the topics and play around with that:



@topic = Topic.find(1)

You can use the 'y' function on this also. Try this to display certain attributes of @topic:



>> @topic.body
=> "Check it out, http://railsonedge.blogspot.com"

And you can change these attributes pretty easily also:



>> @topic.body = "And now the body says this."
=> "And now the body says this."
>> @topic.body
=> "And now the body says this."

When you change an attribute, it doesn't automatically save it to the database. But not to worry, it is easy enough:



>> @topic.save
=> true

Additionally, if you are working on a project and don't have a create page set up yet for an object yet, you can just create one from the console:



>> topic = Topic.new :body => "Lorem Ipsum",
:subject => "This Subject", :forum_id => 1, :user_id => 1
=> #<Topic id: nil, forum_id: 1, user_id: 1,
subject: "This Subject", body: "Lorem Ipsum",
created_at: nil, updated_at: nil>
>> topic.save
=> true

That is enough for some basic manipulation of things in your database, but what other things can you do with the console? What if you wanted to see all of the functions that can be used on a specific object? Well, just like when you are using bash, you can use tab to auto-complete:



String.to_ [press tab not enter]

String.to_a String.to_s
String.to_enum String.to_yaml
String.to_json String.to_yaml_properties
String.to_param String.to_yaml_style
String.to_query

Another good thing to use the rails console for is to test out little bits of code to see how they run. Here is a simple example using a loop in the console:



>>@topics = Topic.find(:all)
>> for topic in @topics do
?> puts topic.subject + " - append this"
>> end

My Rails 2.0 Forum Tutorial - append this
New Blog Post Today!! - append this
Welcome! - append this
Question: - append this

As with most terminals, you can access your command history by using the up arrow.
Another cool thing, when you are messing around with routing, you can check things out quickly with app.url_for and app.get:



>> app.url_for(:controller => 'forums', :action => 'index')
=> "http://www.example.com/forums"

>> app.get '/forums/1/topics'
=> 200

>> app.get '/invalid/url'
=> 404


The underscore('_') character will return the value of the last statement:



>> 5 + 9
=> 14
>> _
=> 14
>> _ + 6
=> 20
>> _
=> 20

Well, that is it for now. Let me know what other uses you guys (and girls?) have for the console.


Later,


-Ralph

Friday, April 25, 2008

DRY Up Your Views With Layouts For Your Partials

I got comments on my previous post, DRY up your views with nested partials, letting me know that there is a better way. So here is the same thing, done the proper way:


First, I've got this cool set of divs that gives me a nice looking window I will be reusing:


 
<div id="window-top"></div>
<div id="window">
<%= render :partial => 'mypartial' %>
</div>
<div id="window-bottom"></div>

<div id="window-top"></div>
<div id="window">
<%= render :partial => 'myotherpartial' %>
</div>
<div id="window-bottom"></div>

The goal here is to be able to re-use the code for the window multiple times. First, lets create a partial called '_lorem.html.erb':



<p>"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
non proident, sunt in culpa qui officia deserunt mollit anim id est
laborum." </p>

And another called '_blah.html.erb':



<p>blah blah blah blah blah blah blah blah blah blah blah blah blah
blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah
blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah
blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah
blah blah blah </p>


Alright! Now, make one last partial, which will be used for the layout, called '_window.html.erb'. Note that it needs to be created in the same directory as the other partials. This is where you put the code for the cool window you just made.



<div id="window-container">
<div id="window-top-left"></div><div id="window-top">Window Title</div>
<div id="window-top-right"></div>
<div id="window">
<%= yield %>
</div>
<div id="window-bottom-left"></div><div id="window-bottom">
</div><div id="window-bottom-right"></div>
</div>

And now, when you want to render a partial that is inside your window, all you need to do is set the layout to window:



<h1>Test of nested partials</h1>

<%= render :partial => 'blah', :layout => 'window' %>

<%= render :partial => 'lorem', :layout => 'window' %>


And thats it! Enjoy DRYing up your views. Later

-Ralph



EDIT: if you use :layout => 'layouts/window', you can put '_window.html.erb' in /views/application and reuse it throughout the site.

Wednesday, April 16, 2008

DRY up your views with nested partials

NOTE: I got a couple of comments letting me know that the proper way to do this is to use a layout with your partial. Please see this post for instructions. Since making this post, I've been told the partial nesting is generally a bad idea, so use at your own risk.




Got a good one for you today that should help clean up your views and DRY things out. How many times, when building a website, do you use the same code over and over again to build this cool looking window that is re-used over and over?


 
<div id="window-top"></div>
<div id="window">
<%= render :partial => 'mypartial' %>
</div>
<div id="window-bottom"></div>

<div id="window-top"></div>
<div id="window">
<%= render :partial => 'myotherpartial' %>
</div>
<div id="window-bottom"></div>

Well, you can use nested partials to achieve the same effect and re-use your window code. First, lets create a partial called '_lorem.html.erb':



<p>"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
non proident, sunt in culpa qui officia deserunt mollit anim id est
laborum." </p>

And another called '_blah.html.erb':



<p>blah blah blah blah blah blah blah blah blah blah blah blah blah
blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah
blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah
blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah
blah blah blah </p>


Alright! Now, make one last partial, '_window.html.erb'. This is where you put the code for the cool window you just made.
Inside of it, you want to render a partial, and pass in the variable 'name':



<div id="window-container">
<div id="window-top-left"></div><div id="window-top">Window Title</div>
<div id="window-top-right"></div>
<div id="window">
<%= render :partial => name %>
</div>
<div id="window-bottom-left"></div><div id="window-bottom">
</div><div id="window-bottom-right"></div>
</div>

And now, when you want to render a partial that is inside your window, all you need to do is render the 'window' partial, and as a local
variable named 'name', pass in the name of the partial you want rendered inside of it:



<h1>Test of nested partials</h1>

<%= render :partial => 'window', :locals => {:name => 'lorem' } %>

<%= render :partial => 'window', :locals => {:name => 'blah' } %>


And thats it! Enjoy DRYing up your views with nested partials. Later

-Ralph