Example

kyptin Nov 18, 2009 5 thanks

=== code: class Klass def set(string) var_name = "@#{string}" # the '@' is required self.instance_variable_set(var_name, 'bar') end def puts_foo puts @foo end end k = Klass.new k.puts_foo # nil k.set('foo') k.puts_foo # 'bar'

Use hash form of updates argument

wsmith67 Nov 12, 2009 8 thanks

The examples are unfortunate, because passing a string as the updates argument is an invitation to SQL injection attacks. Don't do this!

Billing.update_all("author='#{author}'")

Use the hash form of updates instead:

Billing.update_all(:author => author)

Then the SQL adapter will quote everythi...

Calling migrations within migrations observation

feurio Nov 11, 2009

Following the advice from RISCfuture I could not call a migration from within another migration. I got the following errror message:

NameError Exception: uninitialized constant FixDrunkMistake::CreateExGirlfriendTexts.down

Only after I did a

require 'create_ex_girl_friend_texts' # the migrat...

overwrite

rafaelss Nov 9, 2009

Replacing old value with new one

>> Module.const_set('MY_CONSTANT', 'value')
=> "value"
>> Module::MY_CONSTANT
=> "value"
>> Module.const_set('MY_CONSTANT', 'new value')
(irb):3: warning: already initialized constant MY_CONSTANT
=> "new value"
>> Module::MY_CONSTANT...

define_method with parameters

Oleg Nov 5, 2009 10 thanks

Just to be clear, you can do this:

define_method(:my_method) do |foo, bar| # or even |*args|
# do something
end

This means same as:

def my_method(foo, bar)
# do something
end

If you want to define method with parameters that have default values, you need to get a bit more...