[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[libcaca] Re: Ruby binding



On mar, 2007-11-13 at 09:03 +0100, Sam Hocevar wrote: 
> Is it the Ruby way to have get_width, set_width, width and width= at
> the same time? It looks to me like Perl "let's confuse the user in 53
> different ways" nonsense.

The get_ are really useless, the set_ allow chaining the calls :

a.foo=42 will have 42 value so you can't chain
a.set_foo(42) return self so you can do a.set_foo(42).set_bar(42)

I added the get_ at the end as alias of the other method but I think I
should remove them :)

> > +static VALUE put_char(VALUE self, VALUE x, VALUE y, VALUE ch)
> > +{
> > +    cucul_put_char(_SELF, NUM2INT(x), NUM2INT(y), NUM2ULONG(ch));
> > +    return self;
> > +}
> 
>    Doesn't look like very strong typing. Is there a way to tell Ruby ch
> is an Unicode character? Or does it handle them just like numbers?

Characters does not exist un ruby, you can have either String or
numbers :
$ ruby -e 'c = "abc"[1]; puts c; puts c.class'
98
Fixnum

Ruby still not has fixed the encoding issue, as [] on normal strings
return a byte, size is in bytes, you have each_byte but not each_char...

There is a KCODE telling which is the encoding (between NONE, UTF8, EUC
and SJIS IIRC) but it does not help in a lot of cases, and does not
handle UTF-32.

$ irb
irb(main):001:0> "mélangé".length
=> 9
irb(main):002:0> "mélangé"[1]
=> 195
irb(main):003:0> "mélangé".split("")
=> ["m", "\303", "\251", "l", "a", "n", "g", "\303", "\251"]
irb(main):004:0> $KCODE="UTF8"
=> "UTF8"
irb(main):005:0> "mélangé".length
=> 9
irb(main):006:0> "mélangé"[1]
=> 195
irb(main):007:0> "mélangé".split("")
=> ["m", "é", "l", "a", "n", "g", "é"]

Using the (standard) module 'jcode', we can get a jlength method giving
the correct length, and a each_char method which will actually give you
one string per char, like when using split above.

It does now however fix other methods like reverse :

irb(main):008:0>  "mélangé".reverse                    
=> "\251Ãgnal\251Ãm"

According to
http://redhanded.hobix.com/inspect/futurismUnicodeInRuby.html in the
future [] will return a string instead of a byte, so maybe the correct
think would be to have a string as this parameter and check that this is
only one character.

There are also a lot of external classes handling that, like
http://ruby-unicode.rubyforge.org/doc/files/README.html which improves
the String class but for exemple this one will give you a number for
each character.

> > +static VALUE draw_polyline(VALUE self, VALUE x, VALUE y, VALUE ch)
> 
>    I suggest using one single array of coordinate tuples here. In fact,
> I suggest using coordinate tuples everywhere instead of two x/y values.

Yes I was thinking about doing that but did not want to change the API
too much. Would be really better for the user :)

> > +    rb_define_const(mCucul, "BLACK", INT2FIX(0x00));
> 
>    Please use something like this here:
> 
> #define CUCUL_CONST(x) \
>    rb_define_const(mCucul, #x, INT2FIX(CUCUL_##x))

Yes it would be better 

I had used 
sed -n 's/#define CUCUL_\([^ ]*\) \(0x[^ ]*\) .*/rb_define_const(mCucul,
\1, INT2FIX(\2))/p' cucul.h but indeed this is stupid to use \2 and not
CUCUL_\1 for the value.

-- 
This is the libcaca mailing-list, see http://libcaca.zoy.org/
Trouble unsubscribing? Please contact <sam@zoy.org>
List archives are at http://libcaca.zoy.org/list/