The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Method Check: Param Hacks

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Red Handed

Posts: 1158
Nickname: redhanded
Registered: Dec, 2004

Red Handed is a Ruby-focused group blog.
Method Check: Param Hacks Posted: Feb 14, 2005 2:32 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Red Handed.
Original Post: Method Check: Param Hacks
Feed Title: RedHanded
Feed URL: http://redhanded.hobix.com/index.xml
Feed Description: sneaking Ruby through the system
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Red Handed
Latest Posts From RedHanded

Advertisement

It’s all about finding out where Ruby’s syntax is flexible, isn’t it? So, did you know you can use instance variables or arbitrary code to set the defaults in your method parameters? All executed in the same context as your method.

 class LineArtist
   attr_accessor :distance, :thickness
   def draw( dist = @distance, thick = self.thickness ); end
end

And, check this out—you can refer to earlier parameters when setting defaults!

 class BlogEntry
   attr_accessor :author, :title, :created_at, :content
   def initialize( blog, title, content,
                   created_at = Time.now,
                   author = blog.default_author )
   end
 end

This is especially useful for methods where you’re passing in a set of paths. If some of the paths are deleted, you want to default to an earlier path (or a new path based on an earlier path.)

 class Blog
   def setup( path, template_path = File.join( path, "skel" ) )
   end
 end

Read: Method Check: Param Hacks

Topic: to_sexp, to_c, to_ruby, too_interesting_to_pass_up Previous Topic   Next Topic Topic: How Do You Say Quack in Japanese?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use