# Ideas for (small) internal code enhancement

**URL:** <https://www.ruby-forum.com/t/ideas-for-small-internal-code-enhancement/109021>\
**Category:** Ruby-Gnome 2\
**Created:** [July 10, 2007, 6:19pm UTC](https://www.ruby-forum.com/t/ideas-for-small-internal-code-enhancement/109021 "2007-07-10T18:19:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![blueprint](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/cc9497/32.png) [@blueprint](https://www.ruby-forum.com/u/blueprint)\
**Post date:** [July 10, 2007, 6:19pm UTC](https://www.ruby-forum.com/t/ideas-for-small-internal-code-enhancement/109021/1 "2007-07-10T18:19:13Z")

</div>

- Lately I have seen there are lots of places in the code where we have:

I think all of these cases may be replaced by

return CBOOL2RVAL(…expression…)

I can script the change if you agree, and even add some autotest at  
build time to check there are no new such things[1]?

- I have replaced all “foo == Qnil” by “NIL\_P(foo)”. I think it might  
be useful to add some autotest to keep that clean in the future, what  
do you think?

- Recently, I have also fixed a method implemented with the variable  
arguments list form but attached to the ruby class with a fixed  
arguments list. I can script a check and add an autotest for it.

- Also, I have fixed a couple of memory leaks in gdk-pixbuf-scale, gtk  
returned a newly allocated object and we need special magic on it; for  
that, I would suggest to share the “special magic” with a special  
macro, for example NEWGOBJ2RVAL; and also, I think we need an  
automatized way to track all these memory leaks; what do you think of  
making use of the scheme definitions used in pygtk? they use  
“caller-owns-return #t” to indicate where a new allocation supposed to  
be freed later is performed

- I have seen that when using RARRAY(foo)-\>ptr or other pointer  
dereferencing on a Ruby array, often there is “Check\_Type(foo,  
T\_ARRAY)” before. However, I cannot find “official” rb\_check\_type  
documentation to understand what it does exactly (is there any ruby  
development API documentation available? I repeatedly bump into that).  
Anyway, I suppose Check\_Type is a good thing™, so I’d like to also  
add an autotest to check that all RARRAY(foo)-\>ptr use cases are  
always following a Check\_Type, what do you think?

Phew, enough rg2 brainstorming for today 🙂

[1] would that be ok to write these scripts in perl?

–  
Guillaume C. - [http://zarb.org/~gc/](http://zarb.org/~gc/)

* * *

This [SF.net](http://SF.net) email is sponsored by DB2 Express  
Download DB2 Express C - the FREE version of DB2 express and take  
control of your XML. No limits. Just data. Click to get it now.  
[http://sourceforge.net/powerbar/db2/](http://sourceforge.net/powerbar/db2/)

---

<div class="post-metadata">

**Author:** ![blueprint](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/cc9497/32.png) [@blueprint](https://www.ruby-forum.com/u/blueprint)\
**Post date:** [July 11, 2007, 5:45pm UTC](https://www.ruby-forum.com/t/ideas-for-small-internal-code-enhancement/109021/2 "2007-07-11T17:45:16Z")

</div>

Hi,

On Tue, 10 Jul 2007 18:18:58 +0200  
“Guillaume C.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> - Lately I have seen there are lots of places in the code where we have:
> 
> I think all of these cases may be replaced by
> 
> return CBOOL2RVAL(…expression…)

Agreed. It’s been one of my TODO for a long time 😑

> I can script the change if you agree, and even add some autotest at  
> build time to check there are no new such things[1]?

perl !? why!?  
Do you mean you add the test with perl in CVS?  
If so, I don’t agree with it.  
Because, I don’t think to install ActivePerl to my Windows machine  
when I compile and test it on Windows.  
It should work on every environment which we support unless additional  
tools except ruby and gtk.

# That’s the reason why I rewrote glib-mkenums.

If you mean you just make the converter and use it by youself only,  
I don’t mind what tools you use.

> - I have replaced all “foo == Qnil” by “NIL\_P(foo)”. I think it might  
> be useful to add some autotest to keep that clean in the future, what  
> do you think?

Agree, if you can.

> - Recently, I have also fixed a method implemented with the variable  
> arguments list form but attached to the ruby class with a fixed  
> arguments list. I can script a check and add an autotest for it.

What are “variable arugments list” and “fixed arguments list”?

> - Also, I have fixed a couple of memory leaks in gdk-pixbuf-scale, gtk  
> returned a newly allocated object and we need special magic on it; for  
> that, I would suggest to share the “special magic” with a special  
> macro, for example NEWGOBJ2RVAL; and also, I think we need an  
> automatized way to track all these memory leaks; what do you think of  
> making use of the scheme definitions used in pygtk? they use  
> “caller-owns-return #t” to indicate where a new allocation supposed to  
> be freed later is performed

I don’t agree. we don’t need the macros of “Special cases”.  
In this case, you can define NEWGOBJ2RVAL on the top of  
rbgdk-pixbuf.c like \_SELF().

> - I have seen that when using RARRAY(foo)-\>ptr or other pointer  
> dereferencing on a Ruby array, often there is “Check\_Type(foo,  
> T\_ARRAY)” before. However, I cannot find “official” rb\_check\_type  
> documentation to understand what it does exactly (is there any ruby  
> development API documentation available? I repeatedly bump into that).

In ruby tar-ball, there is README.EXT and Check\_Type is described in it.

> Anyway, I suppose Check\_Type is a good thing™, so I’d like to also  
> add an autotest to check that all RARRAY(foo)-\>ptr use cases are  
> always following a Check\_Type, what do you think?

Agreed.

> Phew, enough rg2 brainstorming for today 🙂

Sure. It’s good idea to iterate this kind of discussions.

–  
.:% Masao M.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

* * *

This [SF.net](http://SF.net) email is sponsored by DB2 Express  
Download DB2 Express C - the FREE version of DB2 express and take  
control of your XML. No limits. Just data. Click to get it now.  
[http://sourceforge.net/powerbar/db2/](http://sourceforge.net/powerbar/db2/)

---

<div class="post-metadata">

**Author:** ![blueprint](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/cc9497/32.png) [@blueprint](https://www.ruby-forum.com/u/blueprint)\
**Post date:** [July 11, 2007, 11:04pm UTC](https://www.ruby-forum.com/t/ideas-for-small-internal-code-enhancement/109021/3 "2007-07-11T23:04:40Z")

</div>

Hi Masao,

Thanks for your reply.

> > I can script the change if you agree, and even add some autotest at  
> > build time to check there are no new such things[1]?
> 
> perl !? why!?

Simply because I feel more comfortable writing small file parsing and  
modification oriented scripts in perl.

> Do you mean you add the test with perl in CVS?

Yes, that was the plan.

> If so, I don’t agree with it.  
> Because, I don’t think to install ActivePerl to my Windows machine  
> when I compile and test it on Windows.

Ah, ok. I wasn’t aware you used windows that much. On linux, perl is a  
no-cost.  
Then it should not be much more difficult to write it in ruby. And  
I’ll get more training for small file-oriented scripts in ruby 🙂

> > - Recently, I have also fixed a method implemented with the variable  
> > arguments list form but attached to the ruby class with a fixed  
> > arguments list. I can script a check and add an autotest for it.
> 
> What are “variable arugments list” and “fixed arguments list”?

A function with parameters “argc, argv, self”, which then needs to be  
binded with rb\_define\_method with -1 as last parameter. I have noticed  
that if if is binded with 2 or whatever as last parameter, it is an  
error which is unnoticed.

> In this case, you can define NEWGOBJ2RVAL on the top of  
> rbgdk-pixbuf.c like \_SELF().

It might not be too special. I can grep 71 "caller-owns-return #t’ in  
_.defs from pygtk (many of them seem to be additional constructors  
though). Also, a newly-allocated GObject_ returned by a function,  
seems to be not a special to gdk-pixbuf case, so I thought it made  
more sense at the GObject level.

> > - I have seen that when using RARRAY(foo)-\>ptr or other pointer  
> > dereferencing on a Ruby array, often there is “Check\_Type(foo,  
> > T\_ARRAY)” before. However, I cannot find “official” rb\_check\_type  
> > documentation to understand what it does exactly (is there any ruby  
> > development API documentation available? I repeatedly bump into that).
> 
> In ruby tar-ball, there is README.EXT and Check\_Type is described in it.

Thanks!

–  
Guillaume C. - [Guillaume Cottenceau](http://zarb.org/~gc/)

* * *

This [SF.net](http://SF.net) email is sponsored by DB2 Express  
Download DB2 Express C - the FREE version of DB2 express and take  
control of your XML. No limits. Just data. Click to get it now.  
[http://sourceforge.net/powerbar/db2/](http://sourceforge.net/powerbar/db2/)

---

<div class="post-metadata">

**Author:** ![blueprint](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/cc9497/32.png) [@blueprint](https://www.ruby-forum.com/u/blueprint)\
**Post date:** [July 13, 2007, 5:43pm UTC](https://www.ruby-forum.com/t/ideas-for-small-internal-code-enhancement/109021/4 "2007-07-13T17:43:55Z")

</div>

Hi,

On Wed, 11 Jul 2007 23:04:23 +0200  
“Guillaume C.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > - Recently, I have also fixed a method implemented with the variable  
> > > arguments list form but attached to the ruby class with a fixed  
> > > arguments list. I can script a check and add an autotest for it.
> > 
> > What are “variable arugments list” and “fixed arguments list”?
> 
> A function with parameters “argc, argv, self”, which then needs to be  
> binded with rb\_define\_method with -1 as last parameter. I have noticed  
> that if if is binded with 2 or whatever as last parameter, it is an  
> error which is unnoticed.

Ah, yes. Good idea.

> > In this case, you can define NEWGOBJ2RVAL on the top of  
> > rbgdk-pixbuf.c like \_SELF().
> 
> It might not be too special. I can grep 71 "caller-owns-return #t’ in  
> _.defs from pygtk (many of them seem to be additional constructors  
> though). Also, a newly-allocated GObject_ returned by a function,  
> seems to be not a special to gdk-pixbuf case, so I thought it made  
> more sense at the GObject level.

Start with gdk-piixbuf.c and some other libraries. It’s really general,  
then move the macro to ruby-glib2.

–  
.:% Masao M.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

* * *

This [SF.net](http://SF.net) email is sponsored by DB2 Express  
Download DB2 Express C - the FREE version of DB2 express and take  
control of your XML. No limits. Just data. Click to get it now.  
[http://sourceforge.net/powerbar/db2/](http://sourceforge.net/powerbar/db2/)
