[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[C ext to Ruby] how to output a octal coded number ???

unbewust

8/14/2007 8:47:00 AM

i write a C extension to Ruby, some methods are dealing with octal
number (bytes in english ?) for the file permission :

ie something like that :

0777 or 0644 the first 0 (zero) meaning the following is a number
coded in octal.


that's OK for input of octal number if for example, from ruby the user
write :

this_file.perms = 0777 (then with the first 0)

i get the right number

however, for the time being, i'm returning, for the same value :

777 instead of 0777 when the user wants to read the perms,, then my
question how to return within a C est to Ruby an integer coded in
octal ???

thought, at that time i'm usinbg INT2FIX ( )

best

Yvon

8 Answers

Eric Hodel

8/14/2007 9:32:00 AM

0

On Aug 14, 2007, at 01:49, unbewust wrote:

> i write a C extension to Ruby, some methods are dealing with octal
> number (bytes in english ?) for the file permission :
>
> ie something like that :
>
> 0777 or 0644 the first 0 (zero) meaning the following is a number
> coded in octal.
>
>
> that's OK for input of octal number if for example, from ruby the user
> write :
>
> this_file.perms = 0777 (then with the first 0)
>
> i get the right number
>
> however, for the time being, i'm returning, for the same value :
>
> 777 instead of 0777 when the user wants to read the perms,, then my
> question how to return within a C est to Ruby an integer coded in
> octal ???
>
> thought, at that time i'm usinbg INT2FIX ( )

The easiest way will probably be to use sprintf. From C, use
rb_f_sprintf.

--
Poor workers blame their tools. Good workers build better tools. The
best workers get their tools to do the work for them. -- Syndicate Wars



unbewust

8/14/2007 12:57:00 PM

0

On 14 ao?t, 11:31, Eric Hodel <drbr...@segment7.net> wrote:
> On Aug 14, 2007, at 01:49, unbewust wrote:
>
>
>
> > i write a C extension to Ruby, some methods are dealing with octal
> > number (bytes in english ?) for the file permission :
>
> > ie something like that :
>
> > 0777 or 0644 the first 0 (zero) meaning the following is a number
> > coded in octal.
>
> > that's OK for input of octal number if for example, from ruby the user
> > write :
>
> > this_file.perms = 0777 (then with the first 0)
>
> > i get the right number
>
> > however, for the time being, i'm returning, for the same value :
>
> > 777 instead of 0777 when the user wants to read the perms,, then my
> > question how to return within a C est to Ruby an integer coded in
> > octal ???
>
> > thought, at that time i'm usinbg INT2FIX ( )
>
> The easiest way will probably be to use sprintf. From C, use
> rb_f_sprintf.
>
> --
> Poor workers blame their tools. Good workers build better tools. The
> best workers get their tools to do the work for them. -- Syndicate Wars


OK, thanks, but i don't want to print it just have it as a return
value then you mean i can use sprintf to print to stdout and the value
will be cached by Ruby ?

this is for cosmetic purpose because my "octal" is well writen except
the first "0" (zero) missing...

Tim Pease

8/14/2007 2:07:00 PM

0

On 8/14/07, unbewust <yvon.thoraval@gmail.com> wrote:
>
> 777 instead of 0777 when the user wants to read the perms,, then my
> question how to return within a C est to Ruby an integer coded in
> octal ???
>
> thought, at that time i'm usinbg INT2FIX ( )
>

Ruby displays integers in base 10 format. I do not believe there is a
way to get it to display 511 in octal (0777) without using sprintf as
Eric Hodel mentioned in his reply.

However, if you just want to return from a C function and have the
number look pretty in your C source code, then this will work:

return INT2FIX( 0777 );

But I am not sure if that is what you mean?

Blessings,
TwP

Simon Krahnke

8/14/2007 2:55:00 PM

0

* unbewust <yvon.thoraval@gmail.com> (10:46) schrieb:

> that's OK for input of octal number if for example, from ruby the user
> write :
>
> this_file.perms = 0777 (then with the first 0)
>
> i get the right number
>
> however, for the time being, i'm returning, for the same value :

> 777 instead of 0777 when the user wants to read the perms,, then my
> question how to return within a C est to Ruby an integer coded in
> octal ???

A integer in ruby is a numeric value, it's not octal or decimal.

> thought, at that time i'm usinbg INT2FIX ( )

A string representation of a number can be octal or decimal or whatever.
The Ruby as well and the C library have means to convert between numbers
and its string representations.

num = "775".to_i(8)
dec = num.to_s #> "509"
oct = num.to_s(8) #> "775"

mfg, simon .... l

> best
>
> Yvon

BTW: The german word unbewusst is written with two s. :-)

unbewust

8/14/2007 3:31:00 PM

0

On 14 ao?t, 16:55, Simon Krahnke <overl...@gmx.li> wrote:
> * unbewust <yvon.thora...@gmail.com> (10:46) schrieb:
>
> > that's OK for input of octal number if for example, from ruby the user
> > write :
>
> > this_file.perms = 0777 (then with the first 0)
>
> > i get the right number
>
> > however, for the time being, i'm returning, for the same value :
> > 777 instead of 0777 when the user wants to read the perms,, then my
> > question how to return within a C est to Ruby an integer coded in
> > octal ???
>
> A integer in ruby is a numeric value, it's not octal or decimal.
>
> > thought, at that time i'm usinbg INT2FIX ( )
>
> A string representation of a number can be octal or decimal or whatever.
> The Ruby as well and the C library have means to convert between numbers
> and its string representations.

OK thanks

the reason for me to output as an octal number it is an habit when
talking about perms...
i'll look at the C library to output that the correct way.

> num = "775".to_i(8)
> dec = num.to_s #> "509"
> oct = num.to_s(8) #> "775"

i've allready a way to output my number as an octal "775" BUT i'll
like having better "0775" because in the C language the first 0 means
octal and i ask for xhen input .

> mfg, simon .... l
>
> > best
>
> > Yvon
>
> BTW: The german word unbewusst is written with two s. :-)

yes i know, i didn't cacht my misspelling ;-)

vielen dank ;-)

Yvon

Phrogz

8/14/2007 4:41:00 PM

0

On Aug 14, 6:56 am, unbewust <yvon.thora...@gmail.com> wrote:
> On 14 ao?t, 11:31, Eric Hodel <drbr...@segment7.net> wrote:
> > The easiest way will probably be to use sprintf. From C, use
> > rb_f_sprintf.

> OK, thanks, but i don't want to print it just have it as a return
> value then you mean i can use sprintf to print to stdout and the value
> will be cached by Ruby ?

Despite the name, sprintf does not actually print the value to stdout.
I prefer its more terse version, String#%:

C:\>irb
irb(main):001:0> n = 509
=> 509
irb(main):002:0> s = "%04o" % n
=> "0775"

For more info, "ri sprintf" or "ri String#%"

Eric Hodel

8/14/2007 6:34:00 PM

0

On Aug 14, 2007, at 05:59, unbewust wrote:
> On 14 août, 11:31, Eric Hodel <drbr...@segment7.net> wrote:
>> On Aug 14, 2007, at 01:49, unbewust wrote:
>>> i write a C extension to Ruby, some methods are dealing with octal
>>> number (bytes in english ?) for the file permission :
>>>
>>> ie something like that :
>>>
>>> 0777 or 0644 the first 0 (zero) meaning the following is a number
>>> coded in octal.
>>>
>>> that's OK for input of octal number if for example, from ruby the
>>> user
>>> write :
>>>
>>> this_file.perms = 0777 (then with the first 0)
>>>
>>> i get the right number
>>>
>>> however, for the time being, i'm returning, for the same value :
>>>
>>> 777 instead of 0777 when the user wants to read the perms,, then my
>>> question how to return within a C est to Ruby an integer coded in
>>> octal ???
>>>
>>> thought, at that time i'm usinbg INT2FIX ( )
>>
>> The easiest way will probably be to use sprintf. From C, use
>> rb_f_sprintf.
>
> OK, thanks, but i don't want to print it just have it as a return
> value then you mean i can use sprintf to print to stdout and the value
> will be cached by Ruby ?

sprintf returns a String. You can print it with puts if you want, or
use it later.

--
Poor workers blame their tools. Good workers build better tools. The
best workers get their tools to do the work for them. -- Syndicate Wars



Simon Krahnke

8/15/2007 10:55:00 AM

0

* unbewust <yvon.thoraval@gmail.com> (17:31) schrieb:

>> num = "775".to_i(8)
>> dec = num.to_s #> "509"
>> oct = num.to_s(8) #> "775"
>
> i've allready a way to output my number as an octal "775" BUT i'll
> like having better "0775" because in the C language the first 0 means
> octal and i ask for xhen input .

I don't quite understand the purpose of your extension. Normally you do
interaction with the user in ruby code.

mfg, simon .... l