Rails 3: how to undo rails generate

posted by Arkham on July 29th, 2011

Have you ever run a

rails generate controller UsersController

and seen your app folder filled with useless files?

create app/controllers/users_controller_controller.rb
invoke erb
create app/views/users_controller
invoke test_unit
create test/functional/users_controller_controller_test.rb
invoke helper
create app/helpers/users_controller_helper.rb
invoke test_unit
create test/unit/helpers/users_controller_helper_test.rb
invoke assets
create app/assets/javascripts/users_controller.js.coffee
invoke scss
create app/assets/stylesheets/users_controller.css.scss

Eww, that’s gross.
To undo that rails generate, simply run:

rails destroy controller UserController

and rails will destroy all those ugly files for you!

remove app/controllers/users_controller_controller.rb
invoke erb
remove app/views/users_controller
invoke test_unit
remove test/functional/users_controller_controller_test.rb
invoke helper
remove app/helpers/users_controller_helper.rb
invoke test_unit
remove test/unit/helpers/users_controller_helper_test.rb
invoke assets
remove app/assets/javascripts/users_controller.js.coffee
invoke scss
remove app/assets/stylesheets/users_controller.css.scss

Tags: , ,

Ruby, difference from instance_eval and class_eval

posted by Arkham on July 5th, 2011

String.instance_eval { def foo; p "bar"; end }
String.foo = "bar"
String.class_eval { def foo; p "bar"; end }
"sausage".foo = "bar"

So, using instance_eval adds a class method.
On the other hand, using class_eval adds a instance method.

Why?

class_eval is a method of the Module class, meaning that the receiver will be a module or a class. The block you pass to class_eval is evaluated in the context of that class. Defining a method with the standard def keyword within a class defines an instance method, and that’s exactly what happens here.

instance_eval, on the other hand, is a method of the Object class, meaning that the receiver will be an object. The block you pass to instance_eval is evaluated in the context of that object. That means that String.instance_eval is evaluated in the context of the String object. Remember that a class name is simply a constant which points to an instance of the class Class. Because of this fact, defining a method in the context of Class instance referenced by String creates a class method for the String class.

Tags: ,

Rails 3: difference between has_many :through and has_and_belongs_to_many

posted by Arkham on May 24th, 2011

From Rails guides:

The simplest rule of thumb is that you should set up a has_many :through relationship if you need to work with the relationship model as an independent entity. If you don’t need to do anything with the relationship model, it may be simpler to set up a has_and_belongs_to_many relationship (though you’ll need to remember to create the joining table in the database).

Tags: , , ,

Immigrant Song – Traduzione

posted by Arkham on April 24th, 2011

Immigrant Song

Ah-ah-ahhh-ah! Ah-ah-ahhh-ah!
We come from the land of the ice and snow,
From the midnight sun where the hot springs blow.
Hammer of the Gods will drive our ships
To new lands to fight the horde
Singing and crying: “Valhalla I am coming.”

On we sweep with, with threshing oar.
Our only goal will be the western shore.

Ah-ah-ahhh-ah! Ah-ah-ahhh-ah!
An’ we come from the land of the ice and snow,
From the midnight sun where the hot springs blow.
How soft your fields so green
Can whisper tales of gore of how we calmed
The tides of war. We are your over lords.

On we sweep with threshing oar.
Our only goal will be the western shore.

So now you better stop and rebuild all your ruins.
For peace and trust can win the day despite of all
Your losing.

Ooh-ooh. Ooh-ooh. Ooh-ooh. Ooh-ooh.
Ooh-ooh. Ooh-ooh. Ah hah hah hah hah hah.
Ooh-ooh. Ooh-ooh. Ooh-ooh. Ooh-ooh.
Ooh-ooh. Ooh-ooh. Ooh-ooh. Ooh-ooh.
Ooh-ooh

La canzone del ramingo

Ah-ah-ahhh-ah! Ah-ah-ahhh-ah!
La nostra è la terra della neve e del ghiaccio,
dove le calde fonti sgorgano sotto al sole di mezzanotte.
Il martello degli dei guiderà le nostre navi
verso nuove terre e nuove guerre,
mentre cantiamo e urliamo: “Valhalla, arriviamo!”

Avanziamo contro vento, sferzando il mare
Quella è la nostra meta, la costa occidentale.

Ah-ah-ahhh-ah! Ah-ah-ahhh-ah!
La nostra è la terra della neve e del ghiaccio,
dove le calde fonti scorrono sotto al sole di mezzanotte.
E con che dolcezza i verdi pascoli
sanno sussurrare storie di sangue e morte,
storie di guerre ormai antiche a cui abbiamo posto fine.
Noi, i signori supremi.

Avanziamo contro vento, sferzando il mare
Quella è la nostra meta, la costa occidentale.

E’ meglio se vi fermate e ricostruite le vostre rovine,
perché la pace e la fiducia possono portarvi grandi vittorie
dalle vostre sconfitte.

Ooh-ooh. Ooh-ooh. Ooh-ooh. Ooh-ooh.
Ooh-ooh. Ooh-ooh. Ah hah hah hah hah hah.
Ooh-ooh. Ooh-ooh. Ooh-ooh. Ooh-ooh.
Ooh-ooh. Ooh-ooh. Ooh-ooh. Ooh-ooh.
Ooh-ooh

Tags: , , ,

The bravery of being out of range

posted by Arkham on April 6th, 2010

Read this: http://collateralmurder.org/

Roger Waters – The bravery of being out of range

You have a natural tendency
To squeeze off a shot
You’re good fun at parties
You wear the right masks
You’re old but you still
Like a laugh in the locker room
You can’t abide change
You’re at home on the range

You opened your suitcase
Behind the old workings
To show off the magnum
You deafened the canyon
A comfort a friend
Only upstaged in the end
By the Uzi machine gun

Does the recoil remind you
Remind you of sex
Old man what the hell you gonna kill next
Old timer who you gonna kill next

I looked over Jordan and what did I see
Saw a U.S. Marine in a pile of debris
I swam in your pools
And lay under your palm trees
I looked in the eyes of the Indian
Who lay on the Federal Building steps

And through the range finder over the hill
I saw the front line boys popping their pills
Sick of the mess they find
On their desert stage
And the bravery of being out of range
Yeah the question is vexed

Old man what the hell you gonna kill next
Old timer who you gonna kill next

Hey bartender over here
Two more shots
And two more beers
Sir turn up the TV sound
The war has started on the ground

Just love those laser guided bombs
They’re really great
For righting wrongs
You hit the target
And win the game
From bars 3,000 miles away
3,000 miles away

We play the game
With the bravery of being out of range

We zap and maim
With the bravery of being out of range

We strafe the train
With the bravery of being out of range

We gain terrain
With the bravery of being out of range
With the bravery of being out of range

We play the game
With the bravery of being out of range

Tags: , , ,