From: Bill Guindon Date: 2005-10-23T07:35:51+09:00 Subject: Re: A comparison by example of keyword argument styles On 10/21/05, Berger, Daniel wrote: > > -----Original Message----- > > From: Trans [mailto:transfire@gmail.com] > > Sent: Friday, October 21, 2005 8:12 AM > > To: ruby-talk ML > > Subject: Re: A comparison by example of keyword argument styles > > If keywords integrate the interface too tightly > > with the code, then why tie them to the interface? Also, I > > imagine there many be many more keyword arguments then > > ordered args, this will make for some very LONG constructors: > > > > def stadium( height:, seating_capacity:, location:, color:, > > sports:, concessions:, extra_features:, indoor:, yadayadayada: ) > I'm glad you brought this example up. What do you do when you have a > ton of accessors in your class? I have just such a case with the Format > class in the spreadsheet package, where you have "font_name", > "font_size", "border_color", etc. About 30 accessors I think. > > The solution here is NOT keywords arguments (regardless of > implementation), where you would end up with ridiculously long > constructors, as you mentioned. It's the old "yield self if > block_given?" trick: > > class Format > attr_accessor :font_color, :font_size, :bold # ... > def initialize > yield self if block_given? > end > end > > format = Format.new do |f| > f.font_color = "blue" > f.font_size = 10 > f.bold = true > ... > end Has any thought been given to defining the parameters and keywords using something similar to attr_accessor? Seems that would reduce the long constructor problem, and would be fairly developer friendly. It's always seemed strange to me that the two formats differ, seems a nuisance when you've built up either the attr_accessor list, or your parameter list, and you have to hand massage one to the other. class Format attr_accessor :font_face, :font_color, :font_size, :bold, :italic def initialize parameters :font_face='arial' keywords :font_color='black', :font_size, :bold=false, :italic=false ... end end format = Format.new('verdana') format = Format.new(font_size: 1, bold: true) format = Format.new('verdana', italic: true, font_size: 10) granted, functon calls and constructors could still get quite lengthy. disclaimer: this is not a thought that I'm in love with, just one that crossed my mind. > Regards, > > Dan [OT] many thanks to those who put together the conference, and the videos. -- Bill Guindon (aka aGorilla)