Refactor
:my
=>
'code'
Codes
Refactorings
Popular
Best
Submit
Spam
Account
Logout
Login
JavaScript doesn't seem to be activated, expect things to be ugly and sloppy!
More Jobs
Recent
Keeping the database clean
Arrays, loops, strings...
Similar virtual attributes and their getters/setter
Check type is XML Serializable
Serena Collage slideshow
Shortest regular expression for matching a subdomain.
Caching Methods
Tab-Switching in jQuery
Apache log file sorting
How to find max, min of three numbers?
Popular
Caching Methods
How to find max, min of three numbers?
Similar virtual attributes and their getters/setter
Shortest regular expression for matching a subdomain.
Arrays, loops, strings...
Check type is XML Serializable
Apache log file sorting
Tab-Switching in jQuery
Serena Collage slideshow
Keeping the database clean
Pastable version of
Show a default value if object or field is empty?
<div style="overflow:auto;border:solid 1px #ccc;background:#000;color:#F8F8F8"> <div class="section"> <pre style="float:left;margin:0 10px;border-right:0;color:#666;">1 2 3 4 5 6 7 8 9 10 11 12 13 14</pre> <pre class="sunburst"><span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">#</span> this method returns a default when the object does not exist or the value given in the block does not exist.</span> <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">#</span></span> <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">#</span> Example:</span> <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">#</span> user = User.find(:first)</span> <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">#</span> user2 = nil</span> <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">#</span> </span> <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">#</span> show_value_or_default(user) {|user| user.phone} -> 'phonenumber of user' or '-' of phone is nil or ''.</span> <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">#</span> show_value_or_default(user2,"leeg") {|user| user.phone} -> 'leeg'</span> <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">#</span> </span> <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">#</span> show_value_or_default(user) {|user| i18n(user.gender)} -> i18n version of the gender. </span> <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">#</span> </span> <span style="color:#E28964;">def</span> <span style="color:#89BDFF;">show_value_or_default</span>(<span style="color:#3E87E3;">object<span style="color:#3E87E3;">,</span> default <span style="color:#E28964;">=</span> <span style="color:#65B042;"><span style="color:#65B042;">"</span>-<span style="color:#65B042;">"</span></span></span>) (object.<span style="color:#89BDFF;">nil?</span> <span style="color:#E28964;">or</span> <span style="color:#E28964;">yield</span>(object).<span style="color:#89BDFF;">blank?</span>) <span style="color:#E28964;">?</span> default : <span style="color:#E28964;">yield</span>(object) <span style="color:#E28964;">end</span> </pre> </div> </div> <a href="http://refactormycode.com/codes/50-show-a-default-value-if-object-or-field-is-empty" style="color:#fff" title="As seen on RefactorMyCode.com"><img alt="Small_logo" src="http://refactormycode.com/images/small_logo.gif" style="border:0" /></a>