Find symlink target path
To find the target of a symlink, use File.readlink
Community contributions, tips, and corrections to the documentation. (1708 notes)
To find the target of a symlink, use File.readlink
Imagine the following case. You have two landing pages, one generic one, and an account specific one. The urls are as follows:
map.landing 'landing', :controller => 'landing', :action => 'index'
map.account_landing 'accounts/:account_id/landing', :controller => 'landing', :action => 'index'...
If you overwrite the to_param method in your model class such that it does not begin with its id, you can be in for a nasty surprise:
==== Example class User def to_param self.login end ... end
Let's say you have a user called "bob", than you might think this works:...
I need that program :)
File.basename provides what File.dirname omits.
Be aware that the order of arguments for this method is the opposite of File.join:
File.expand_path('foo', '/bar') # => "/bar/foo"
File.join('foo', '/bar') # => "foo/bar"
There are other more specific methods defined in the IO class: IO.open for files, IO.popen for pipes.
Instead of passing in a formatter block, you can always create a subclass that defines the format:
require 'logger'
class MyLogger < Logger
def format_message(severity, datetime, progname, msg)
"[%s %s] %s\
" % [ severity, datetime.strtftime("%H:%M"), msg ] end end
This...
Note that the use of Symbol#to_proc requires either Rails or Ruby 1.8.7. Prior versions will show:
['a', 'b', 'c'].collect(&:capitalize)
# => TypeError: wrong argument type Symbol (expected Proc)
You may write something like this: >> ['a', 'b', 'c'].collect{|letter| letter.capitalize} => ["A", "B", "C"]
But it looks so much nicer this way: >> ['a', 'b', 'c'].collect(&:capitalize) => ["A", "B", "C"]
Beware, that using strings as association names, when giving Hash to :include will render errors:
The error occurred while evaluating nil.name
So, :include => ['assoc1', 'assoc2' ] will work, and :include => [ {'assoc1' => 'assoc3'}, 'assoc2'] won't. Use symbols:
==== Proper form
:include => [...
Real value: HTML_ESCAPE = { '&' => '&', '>' => '>', '<' => '<', '"' => '"' }
Date, Time and DateTime may have different formats defined.
If you do: @user.created_at.to_formatted_s(:long_ordinal) You will get (or something): April 16th, 2009 22:03 But if you do: @user.created_at.to_date.to_formatted_s(:long_ordinal) You will get: April 16th, 2009 So, be sure...
Other languages use the term throw for raising exceptions, but Ruby has a specific raise call for that.
Don't forget to add indexes to HATM table:
add_index :developers_projects, [:developer_id, :project_id]
http://www.spacevatican.org/2008/8/19/fun-with-class-variables
"When you set a class_inheritable_array or a class_inheritable_hash you are actually concatenating (or merging) with the value inherited from the super class."
==== Code example
class Base
class_inheritable_hash :attrs
se...
It comes up with an error about white_list_sanitizer undefined in the class you're using it in. To get around this, use:
ActionController::Base.helpers.strip_tags('string')
To shorten this, add something like this in an initializer:
class String
def strip_tags
ActionController::...
It comes up with an error about white_list_sanitizer undefined in the class you're using it in. To get around this, use:
ActionController::Base.helpers.strip_links('string')
To shorten this, add something like this in an initializer:
class String
def strip_links
ActionController...
It comes up with an error about white_list_sanitizer undefined in the class you're using it in. To get around this, use:
ActionController::Base.helpers.sanitize('string')
To shorten this, add something like this in an initializer:
class String
def sanitize
ActionController::Base...
rafaelrosafu, the correct tink to ActiveRecord's "new_record?" method is: