February 2012
9 posts
2 tags
Around Alias
You can write an Around Alias in three simple steps:
You alias a method.
You redefine it.
You call the old method from the new method.
Example:
class String
alias :orig_length :length
def length
"Length of string '#{self}' is: #{orig_length}"
end
end
"abc".length
#=> "Length of string 'abc' is: 3"
This tip was submitted by Nimesh Nikum.
1 tag
Rails' include_root_in_json
When rendering JSON from your controllers (or when using to_json directly), Rails 3.1 and above won’t include the root element in the output:
post = Post.first
# => #<Post id: 1, title: "My first blogpost" ...
post.to_json
# => "{\"id\":1,\"title\":\"My first blogpost\", ...}"
To include the root element (post in this example), set ActiveRecord::Base.include_root_in_json to...
2 tags
Naming the Process
You can set the name of the current Ruby process, the one that you would see from the ps command for example.
Simply assign a string to the global variable $PROGRAM_NAME:
$PROGRAM_NAME = 'Hello from Rubyland!'
puts $0 # This is an alias for the same thing.
This is a great way for long running scripts or daemon processes to communicate status information to people who are looking in on...
2 tags
Making class methods private
This does not work:
class Foo
private
def self.bar
end
end
Foo.bar will be public.
To make it private, you can use Module#private_class_method:
class Foo
def self.bar
end
private_class_method :bar
end
…or define it differently:
class Foo
class << self
private
def bar
end
end
end
This tip was submitted by two-bit-fool.
2 tags
Clear your development logs automatically when...
This snippet simply clears your logs when they are too large. Every time you run rails server or rails console it checks log sizes and clears the logs for you if necessary.
# config/initializers/clear_logs.rb
if Rails.env.development?
MAX_LOG_SIZE = 2.megabytes
logs = File.join(Rails.root, 'log', '*.log')
if Dir[logs].any? {|log| File.size?(log).to_i > MAX_LOG_SIZE }
...
3 tags
Quickly convert an Array to a Hash
As an example, let’s say you want to create an index of ActiveRecord objects by their id. Use the Hash constructor that accepts an Array of key-value pairs and do it in one line:
posts_by_id = Hash[*Post.all.map{ |p| [p.id, p] }.flatten]
This tip was submitted by http://Fullware.net/.
2 tags
Initialize Objects with a Hash
If you want to initialize new instances of your class and specify values for its attributes directly, you can implement an initialize method with a lot of arguments. Or you can use a Hash - the Rails way:
my_puppy = Dog.new(:name => "Luke", :birthdate => Time.now)
You can do this by using a general initialize implementation:
module GeneralInit
def initialize(*h)
if h.length...
2 tags
Two ways of joining Array elements
You can use Array#join to concatenate Array elements to a String:
[1, 2, 3].join('+')
# => "1+2+3"
Array#* has the same effect when you pass it a String:
[1, 2, 3] * '+'
# => "1+2+3"
This tip was submitted by Ciur Eugen.