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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Berger, Daniel (Guest)
on 2006-05-03 20:57
(Received via mailing list)
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.
Robert K. (Guest)
on 2006-05-03 20:57
(Received via mailing list)
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
Stephen W. (Guest)
on 2006-05-03 20:58
(Received via mailing list)
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
Joel VanderWerf (Guest)
on 2006-05-03 20:58
(Received via mailing list)
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;
};
This topic is locked and can not be replied to.