Shortcut for .pack("p*").unpack("l").first?


#1

Hi all,

If I want to get the underlying pointer address of a
Ruby string, I can do this:

string = “hello”
[string].pack(“p*”).unpack(“l”).first # 43329024

I thought there was a shortcut for this. Originally I thought
perhaps object_id was identical, but that doesn’t seem to be
the case. Maybe that was immediate types only.

Anyway, is there a shorter way? Or am I remembering
incorrectly?

Thanks,

Dan

This communication is the property of Qwest and may contain confidential
or
privileged information. Unauthorized use of this communication is
strictly
prohibited and may be unlawful. If you have received this communication
in error, please immediately notify the sender by reply e-mail and
destroy
all copies of the communication and any attachments.


#2

2006/5/1, Berger, Daniel removed_email_address@domain.invalid:

Hi all,

If I want to get the underlying pointer address of a
Ruby string, I can do this:

What do you want with a memory address in Ruby-land? If you’re in an
extension then there’s probably an easier way to get at the address.

string = “hello”
[string].pack(“p*”).unpack(“l”).first # 43329024

I thought there was a shortcut for this. Originally I thought
perhaps object_id was identical, but that doesn’t seem to be
the case. Maybe that was immediate types only.

If you want to identify an individual object then of course object_id
is the appropriate means.

Kind regards

robert


#3

Berger, Daniel wrote:

If I want to get the underlying pointer address of a

Insert standard confusion about someone worried about pointers in Ruby.
Assume you know what you want.

Ruby string, I can do this:

string = “hello”
[string].pack(“p*”).unpack(“l”).first # 43329024

Anyway, is there a shorter way? Or am I remembering
incorrectly?

Why don’t you just add it to String?

irb(main):001:0> class String; def blah;
[self].pack(“p*”).unpack(“l”).first; end; end
=> nil
irb(main):002:0> “hello”.blah
=> 43862144

–Steve


#4

Berger, Daniel wrote:

the case. Maybe that was immediate types only.
Even if you can munge object_id and get a pointer to the object, still,
in the case of strings, you have to dereference from a field in the
object to get to the actual string storage:

struct RString {
struct RBasic basic;
long len;
char *ptr;
union {
long capa;
VALUE shared;
} aux;
};