# Best way to do helpers for a constructor

**URL:** <https://www.ruby-forum.com/t/best-way-to-do-helpers-for-a-constructor/67598>\
**Category:** Ruby\
**Created:** [September 5, 2006, 3:38pm UTC](https://www.ruby-forum.com/t/best-way-to-do-helpers-for-a-constructor/67598 "2006-09-05T15:38:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jasonmorrison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jasonmorrison](https://www.ruby-forum.com/u/jasonmorrison)\
**Post date:** [September 5, 2006, 3:38pm UTC](https://www.ruby-forum.com/t/best-way-to-do-helpers-for-a-constructor/67598/1 "2006-09-05T15:38:42Z")

</div>

What is the best practice for creating helper methods for a  
constructor? In Java (not that I know that much about Java), my  
instinct would be to make helper methods for a constructor be private  
class methods, since they generally don’t rely on the state of the  
object being created. In Ruby, however, it seems you’re not allowed  
to call private class methods from within instance methods.

As a concrete example, here’s part of a solution I submitted for the  
DateRange quiz:

class DateRange

def initialize(\*args)

```
arr = args.collect {|rep| day_num(rep)}

arr.uniq!
arr.sort!

@string = build_string(arr)

```

end

end

day\_num should convert a representation of a day (could be a name,  
abbreviation, or a number) to a day number

build\_string should return a string representing the days in an array  
of day numbers

My question is, how should I define day\_num and build\_string? It  
doesn’t seem like they should be instance methods, since they don’t  
rely on any state. I also want them to be private, since they are not  
part of the class’s specified interface.

In ruby, however, private methods must be called with self as the  
implicit reciever. Since initialize is an instance method, self is an  
instance of class DateRange. When you send messages to an instance of  
DateRange, they look in class DateRange, and then up the inheritance  
heirarchy. Class methods don’t live in the class, though, they live  
in the class’s metaclass, so they won’t be seen when a message is sent  
to an instance of DateRange.

On the other hand, private class methods can be called from other  
class methods, since inside a class method, self refers to the class,  
and when messages are sent to the class, the first place methods are  
looked for is in the metaclass.

In any case, what should I be doing?

Cheers,

Jason

---

<div class="post-metadata">

**Author:** ![jasonmorrison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jasonmorrison](https://www.ruby-forum.com/u/jasonmorrison)\
**Post date:** [September 5, 2006, 9:38pm UTC](https://www.ruby-forum.com/t/best-way-to-do-helpers-for-a-constructor/67598/2 "2006-09-05T21:38:26Z")

</div>

On 9/5/06, Jason M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What is the best practice for creating helper methods for a  
> constructor? In Java (not that I know that much about Java), my  
> instinct would be to make helper methods for a constructor be private  
> class methods, since they generally don’t rely on the state of the  
> object being created. In Ruby, however, it seems you’re not allowed  
> to call private class methods from within instance methods.

But unlike Java, Ruby doesn’t have constructors, instead it has class  
methods new and allocate which are rarely overidden.

The standard new method calls allocate to get storage for the instance  
and then ‘sends’ initialize to the instance, using a non-standard  
mechanism.

So I’d just make the helper methods private instance methods. Since  
initialize itself is an instance methods it will work. There’s  
nothing written in stone saying that an instance method should rely on  
instance state.

IMHO this difference between Java and Ruby is a good thing. Ruby  
doesn’t have “tyrannical constructors” as Martin F. put in his  
article on Rake.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
