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

**URL:** <https://www.ruby-forum.com/t/shortcut-for-pack-p-unpack-l-first/94393>\
**Category:** Ruby\
**Created:** [May 3, 2006, 6:57pm UTC](https://www.ruby-forum.com/t/shortcut-for-pack-p-unpack-l-first/94393 "2006-05-03T18:57:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [May 3, 2006, 6:57pm UTC](https://www.ruby-forum.com/t/shortcut-for-pack-p-unpack-l-first/94393/1 "2006-05-03T18:57:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [May 3, 2006, 6:57pm UTC](https://www.ruby-forum.com/t/shortcut-for-pack-p-unpack-l-first/94393/2 "2006-05-03T18:57:51Z")

</div>

2006/5/1, Berger, Daniel [removed\_email\_address@domain.invalid](mailto: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

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [May 3, 2006, 6:58pm UTC](https://www.ruby-forum.com/t/shortcut-for-pack-p-unpack-l-first/94393/3 "2006-05-03T18:58:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [May 3, 2006, 6:58pm UTC](https://www.ruby-forum.com/t/shortcut-for-pack-p-unpack-l-first/94393/4 "2006-05-03T18:58:14Z")

</div>

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;  
};
