[wxruby-users] More rubish API

Alex Fenton alex at pressure.to
Wed Nov 5 12:41:50 EST 2008


Kirill

Thanks for the discussion.

Kirill Likhodedov wrote:
> What I also would like to propose is to make set-methods "chainable".
> Currently I have to do the following:
>     style = Wx::RichTextAttr.new
>     style.set_font_face_name("Verdana")
>     style.set_font_size(10)
> I would like to do:
>     style = Wx::RichTextAttr.new
>     style.set_font_face_name("Verdana").set_font_size(10)
> etc.

I'm not generally a fan of method chaining - to me your second example 
is less clear than your first. It's 40 or 50 characters without any 
white space.

I'd use a keyword constructor here, or a more declarative set of statements:

attr = Wx::RichTextAttr.new( :font_face_name => 'Verdana', :font_size => 
10 )

or

attr = Wx::RichAttr.new
attr.font_face_name = 'Verdana'
attr.font_size = 10

The second should already work.
>
> This can be easily done by returning self object after modifying it.

Sure, if you would like this change, you can implement it yourself right 
away. Add something like this to your personal wx_additions library:

class Wx::RichTextAttr
  instance_methods.grep(/^set/) do | method_name |
    old_method = instance_method(method_name)
    define_method(method_name) do | *args |
      old_method.bind(self).call(*args)
      self
    end
  end
end

> I also would like to point that set_text_colour and 
> set_background_colour are named in a british manner (OUR), which is 
> fine for 
> language, but is not traditional for IT, which uses color (OR). It 
> looks reasonable to change or at least alias this.

Hehe. I'm British and this is my single favo_u_rite feature of wxWidgets 
and wxRuby. To replace them in the core lib with nasty US mis-spellings, 
you'll have to prise them from my cold dead fingers. :)

Again, ruby makes it easy to change if it bothers you:

Wx::Color = Wx::Colour
alias :get_background_color :get_background_colour

etc

cheers
alex


More information about the wxruby-users mailing list