Posted by kev
Tue, 06 Sep 2005 00:30:00 GMT
Development, test and production: we all know and love the default rails environments that let us seperate our databases and tests. What do they do, how do they work, and how can we further customize our rails environment?
Please note: As of changeset 2115 the rails environment does not work like this article describes. This article will cover environments in 0.13.1 and earlier. I will release another article this week explaining the new environment system.
Read more...
Posted in Coding, Rails | no comments | no trackbacks
Posted by kev
Fri, 02 Sep 2005 04:36:00 GMT
20:30 < technoweenie> dude, how about a google ad sense section targeting
helper in rails?
http://www.google.com/support/adsense/bin/answer.py?answer=23168&topic=371
20:30 < technoweenie> <% google_ad_sense_section do %> .... <% end %>
20:30 < kevinclark> cool
20:31 < technoweenie> hm
20:31 < technoweenie> the rails helper idea is stupid, dont listne to me
20:33 < kevinclark> yeah, why wouldn't you just hardcode it?
20:33 < technoweenie> because
20:34 < technoweenie> i get off on writing rails helpers
20:34 < kevinclark> its the same amount of characters even
20:34 < technoweenie> it's sickening and i'm trying to stop
20:34 < technoweenie> heh i know!
20:34 < kevinclark> we should form a support group
20:34 < technoweenie> isn't that what this room is?
20:34 < technoweenie> you guys arent doing your jobs
Posted in Musings, Rails, Coding | no comments | no trackbacks
Posted by kev
Wed, 31 Aug 2005 00:54:12 GMT
Bugs have been found in the rails subversion patch I submitted. With the current posted version it will not work if you are using externals. If you’re using the patch and you need a fix, feel free to email me: kevin.clark [at] gmail [dot] com or the nick kevinclark on irc.freenode.net. Additionally, I have put my ticket in the needy patches section as I have found another hiccup that needs to be resolved before being merged with the trunk.
A big thanks to procreate from irc.freenode.net for the heads up!
Posted in Web Dev, Coding, Rails, Ruby, Hacks | no comments | no trackbacks
Posted by kev
Sun, 28 Aug 2005 01:51:00 GMT
Subversion is a wonderful thing. Unfortunately, when using rails code generators it can be annoying adding all the newly generated files by hand. Using script/destroy can be hazardous because subversion will complain the files are not there, but have not been flagged for removal from the repository.
So, I wrote a patch. If it is accepted, you should be able to do things like:
[Dionysus:~/web/testbed] kevincla% script/generate model -c Monkey
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/monkey.rb
A app/models/monkey.rb
create test/unit/monkey_test.rb
A test/unit/monkey_test.rb
create test/fixtures/monkeys.yml
A test/fixtures/monkeys.yml
And all of your happy new code will go into your repository automatically. My patch also accounts for code which has been marked for addition which you would like to delete, so if you just ran the code above and then do script/destroy model -c Monkey it will revert the files and then delete them without any messy svn warnings.
Posted in Coding, Rails, Ruby, Hacks | 1 comment | no trackbacks
Posted by kev
Wed, 24 Aug 2005 07:10:00 GMT
- Create a column to hold the object type (automagically works for ‘type’) in the table corresponding to the model you want to inheirit from.
- Create a model, and set it to inherit like so:
Class BaseClass < ActiveRecord::Base
end
Class ChildClass < BaseClass
end
No, but seriously, you can now use ChildClass.new to create a new ChildClass. It will use the same table as BaseClass. If you want the child class to have more attributes than its parent, add more columns and simply ignore them for the parent. For a full rationale behind why just ignoring them is ok, buy the Agile Rails book. It boils down to convenience and utility. We can easily do this. If you want to impose restrictions on your own to stop base classes or children of a common base class from accessing each others attributes, you’re welcome to add it to the model code.
For more information, including how to set the column to be something other than type, see the api documentation.
Posted in Ruby, Rails, Web Dev | no comments | no trackbacks
Posted by kev
Tue, 23 Aug 2005 04:13:00 GMT
For my latest work, I was required to deploy my rails application on a server running Windows 2003 and MS SQL Server. Normally, I would install Apache on the machine and be happily on my way. Unfortunately, the same machine is host to several websites running on the bastion of Microsoft Webserving, IIS 6.0. This article chronicles my trek through the wilderness to (hopefully) see this project to deployment.
Read more...
Posted in Hacks, Rails, Web Dev | 3 comments | no trackbacks
Posted by kev
Fri, 19 Aug 2005 22:11:00 GMT
Update 8/25: This post has been translated into korean.
Update 12/12: I found a spanish translation today.
Symbols in ruby are an enigma. We use them, but many don’t really understand them.
So really, what is a symbol?
Simply, a symbol is something that you use to represent names and strings. What this boils down to is a way to efficiently have descriptive names while saving the space one would use to generate a string for each naming instance.
The Case of Dr. Jones
Dr. Jones is a Psycologist. He regularly uses word association tests to diagnose patients and uses ruby to keep track of everything. His first patient, Why, steps up to the plate:
Dr J: Red
Why : Ruby
Dr J: Transportation
Why : Rails
Dr J: Chunky
Why : Bacon
Dr Jones creates a hash to store his data:
why = {"red" => "ruby", "transportation" => "rails", "chunky" => "bacon"}
Dr. Jones’s second patient, Bob, turns in his survey results:
bob = {"red" => "paint", "transportation" => "car", "chunky" => "fat"}
The Problem
After running several hundred word association tests, Dr. Jones begins to realize that he’s running out of memory! On a hunch, Jones runs tests in irb:
> 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
Well look at that, each time he creates a hash to store his information, ruby creates a new string object in a different memory location for each key. Fortunately, there’s an alternative.
Symbols to the Rescue
Unlike strings, symbols of the same name are initialized and exist in memory only once during a session of ruby. Symbols are most obviously useful when you’re going to be reusing strings representing something else. Reproducing Dr. Jones’s tests, we are able to see this directly:
> 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
Using symbols, we’ve used a single memory address to represent the word ruby in our word association tests. Over time, this can save alot of space.
So I’m no shrink, when else will I want to use symbols?
Symbols are useful whenever you’re going to be reusing a word over and over to represent something else, whether its a key in a hash or the method you’re using in an http query. An example from the latest and greatest web framework Ruby on Rails is its use of symbols in routes and links. Rails defines actions within controllers to do things within the framework before rendering a web page, so a link in Rails may look like:
link_to("View Article", :controller => "articles", :action => "show", :id => 1)
When an application may have hundreds of links, or atleast hundreds of references to different actions and controllers, it is significantly more efficient to use symbols than strings.
Finally, its important to note that the usefulness of symbols is not restricted to keys in hashes. For example, if one was writing a http client or server they might use get and post several times within their application, and it might be appropriate to use:
do_this if query == :get
...
send_message_to_server(:post,filename)
Any time a string could be used over and over, a symbol may be a good candidate for replacement.
Updates
In #ruby-lang on Freenode (irc.freenode.net) Aria and nome presented helpful additions to this article.
11:58 < Aria> Also, the entirely realistic reasoning for using symbols: If you
are going to refer to a method name, use a symbol. Because /by
defining the method/, the symbol exists anyway.
12:03 < nome> kevinclark: the intention of symbols are for identification of
(user-level, primarily) constructs: a slot in a hash, a method,
an option, etc.
Also note Aria’s response to Geoff’s question in the comments:
Geoff -
I'd be interested in knowing exactly how
much memory 1,000 strings ("red") uses over :red.
And remember, outside of Rails, "red" != :red
Aria -
How much memory? 20 bytes per object, plus storage for the data, 3 bytes,
plus the length storage (4 byes)—so 27,000 bytes or so.
Versus one copy of the entry in the symbol table, which is likely to be just
a few bytes (I could check, but I know for certain it’s in the tens, not tens
of thousands of bytes range.)
Jim Weirich notes:
I (generally) use the following rule on string vs symbols:
(1) If the contents (i.e. the sequence of characters) of the object is important, use a String.
(2) If the identity of the object is important, use a Symbol.
Reports of errors and omissions are welcome and should be sent to kevin [dot] clark [at] gmail [dot] com
Posted in Coding, Ruby | 33 comments | 5 trackbacks