# Assignment without attr=

**URL:** <https://www.ruby-forum.com/t/assignment-without-attr/120714>\
**Category:** Ruby\
**Created:** [November 20, 2007, 1:21am UTC](https://www.ruby-forum.com/t/assignment-without-attr/120714 "2007-11-20T01:21:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 20, 2007, 1:21am UTC](https://www.ruby-forum.com/t/assignment-without-attr/120714/1 "2007-11-20T01:21:13Z")

</div>

Not even sure what to call this for the thread subject

I want to create a class which is a simple packed set of digits (as a  
string) as the one and only attribute of the class. There will be  
several methods to extract chunks from and format this string, but  
there’s only the one attribute.

For the life of me I can’t figure out how to to populate the dang this  
to start with, without have to go through an attribute. If it were a  
normal multi-attribute object, I’m fine, but in light of there only  
being one attribute, I’m trying to work with it a different way. Maybe  
I’m just going about it all wrong to start with?

The essentials:

class PackedNumber  
def initialize  
@packedNumber  
end

def methodX  
…  
end

def methodY  
…  
end  
end

So, I’d want to work with it like this:  
whatever = PackedNumber.new(‘12345’)  
Or  
whatever = PackedNumber.new  
whatever = ‘12345’ # but I don’t want whatever to be String

I’m trying to avoid the obvious, and maybe this is what is not possible  
whatever.packedNumber = ‘12345’

I tried creating a method just for = but that didn’t fly

def =(chars)  
@packedNumber = chars  
end

Feels pretty lame I can’t see this, but there ya go.

– gw

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 20, 2007, 1:29am UTC](https://www.ruby-forum.com/t/assignment-without-attr/120714/2 "2007-11-20T01:29:47Z")

</div>

Greg W. wrote:

> being one attribute, I’m trying to work with it a different way. Maybe  
> …  
> whatever = PackedNumber.new
> 
> Feels pretty lame I can’t see this, but there ya go.
> 
> – gw

maybe I’m missing something obvious here, but why not just take the  
initial value as an argument to initialize?

class PackedNumber  
def initialize(packed\_number)  
@packedNumber = packed\_number  
end  
# other stuff  
end

pn = PackedNumber.new(‘12345’)

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 20, 2007, 1:35am UTC](https://www.ruby-forum.com/t/assignment-without-attr/120714/3 "2007-11-20T01:35:38Z")

</div>

> So, I’d want to work with it like this:  
> whatever = PackedNumber.new(‘12345’)

If that’s all you want, you can do this:

class PackedNumber  
def initialize(string)  
@packednumber = string  
end  
end

HTH,  
Chris

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 20, 2007, 1:38am UTC](https://www.ruby-forum.com/t/assignment-without-attr/120714/4 "2007-11-20T01:38:33Z")

</div>

Greg W. wrote:

> being one attribute, I’m trying to work with it a different way. Maybe  
> …  
> end
> 
> def methodY  
> …  
> end  
> end
> 
> So, I’d want to work with it like this:  
> whatever = PackedNumber.new(‘12345’)

def initialize(num = nil)  
@packedNumber = num  
end

> Or  
> whatever = PackedNumber.new  
> whatever = ‘12345’ # but I don’t want whatever to be String

I don’t think you can do this.

> I’m trying to avoid the obvious, and maybe this is what is not possible  
> whatever.packedNumber = ‘12345’
> 
> I tried creating a method just for = but that didn’t fly
> 
> def =(chars)  
> @packedNumber = chars  
> end

But you could do

def set(chars)  
@packedNumber = chars  
end

whatever = PackedNumber.new

whatever.set ‘12345’

or if you want it even shorter, less ‘method-call-like’

def [] num  
@packedNumber = num  
end

whatever[‘12345’]

-Justin

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 20, 2007, 11:49am UTC](https://www.ruby-forum.com/t/assignment-without-attr/120714/5 "2007-11-20T11:49:38Z")

</div>

Greg W. wrote:

> Â Â whatever = PackedNumber.new(‘12345’)  
> Or  
> Â Â whatever = PackedNumber.new  
> Â Â whatever = ‘12345’ Â # but I don’t want whatever to be String

Given that there is no String#~ this would work:  
class String  
def ~  
PackedNumber.new self  
end  
end

whatever = ~“12345”

That a “little” hackish thogh 😉

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 20, 2007, 1:59am UTC](https://www.ruby-forum.com/t/assignment-without-attr/120714/6 "2007-11-20T01:59:47Z")

</div>

Justin C. wrote:

> or if you want it even shorter, less ‘method-call-like’
> 
> def [] num  
> @packedNumber = num  
> end
> 
> whatever[‘12345’]

Yeah, either of those (.set or []) will be fine.

Tim H. wrote:

> maybe I’m missing something obvious here, but why not just take the  
> initial value as an argument to initialize?

I want to allow assignment at initialization as you say, and an  
alternate method too.

I’m just experimenting/exploring what’s possible.

Thanks guys.

– gw
