# Choosing among object/class/module/top-level methods

**URL:** <https://www.ruby-forum.com/t/choosing-among-object-class-module-top-level-methods/98343>\
**Category:** Ruby\
**Created:** [July 14, 2006, 12:35am UTC](https://www.ruby-forum.com/t/choosing-among-object-class-module-top-level-methods/98343 "2006-07-14T00:35:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [July 14, 2006, 12:35am UTC](https://www.ruby-forum.com/t/choosing-among-object-class-module-top-level-methods/98343/1 "2006-07-14T00:35:18Z")

</div>

Hi, Rubyists!

Whenever I try to make a quick solution program,  
I stop for a moment to think which approach I should take.

Let’s say that you want to make a puzzle-solver (like Sudoku-solver)  
and it’s made up of one ‘solve’ method and several helper methods.

Which is your preferred way among the following?  
And why?

[1]

def solve\_sudoku problem  
…  
end

def helper1 args  
…  
end

def helper2 args  
…  
end

puts solve\_sudoku “15000…00358”

[2]

class Sudoku

```
def initialize problem
...
end

```

private

```
def helper1
...
end

def helper2
...
end

```

end

sudoku = Sudoku.new “15000…00358”  
puts sudoku.solve

[3]

class Sudoku

```
class << self
	def solve problem
	...
	end

private
	def helper1
	...
	end

	def helper2
	...
	end
end

```

end

puts Sudoku.solve “15000…00358”

[4]

module Sudoku

```
class << self
	def solve problem
	...
	end

private

	def helper1
	...
	end

	def helper2
	...
	end
end

```

end

end

puts Sudoku.solve “15000…00358”

I’m a big fan of Object-orientation.  
But if the object’s life-span is as short as Solver.new(problem).solve,  
I’m really hesitant to create an object when a simple method call will  
do.  
Am I thinking of a worthless subjective thing?

Do you have any criteria about going for object/class/module/top-level  
methods?

Thanks.

Sam

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [July 14, 2006, 12:44am UTC](https://www.ruby-forum.com/t/choosing-among-object-class-module-top-level-methods/98343/2 "2006-07-14T00:44:11Z")

</div>

On Fri, 14 Jul 2006, Sam K. wrote:

\<snip various methods i’ve used too\>

> I’m a big fan of Object-orientation. But if the object’s life-span is as  
> short as Solver.new(problem).solve, I’m really hesitant to create an object  
> when a simple method call will do. Am I thinking of a worthless subjective  
> thing?
> 
> Do you have any criteria about going for object/class/module/top-level  
> methods?

in all seriousness this is **precisely** why i just wrote the prototype  
library - i was just facing this question (again) yesterday and couldn’t  
stand  
it anymore. now i can write

solver = prototype{  
@some\_instance\_var

```
 def solve
   something @some_instance_var
 end

 def something arg
 end

```

}

eh voila!

check it out and let me know what you think - it really is designed for  
exactly this purpose.

btw - i’ll be making a new release later today which is cleaned up a lot  
but  
only adds one small new feature so the current one, though messy, should  
be  
fine to play with regarding functionality.

cheers.

-a

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [July 14, 2006, 12:49am UTC](https://www.ruby-forum.com/t/choosing-among-object-class-module-top-level-methods/98343/3 "2006-07-14T00:49:04Z")

</div>

unknown wrote:

> solver = prototype{  
> @some\_instance\_var
> 
> ```
> def solve
> something @some_instance_var
> end
> 
> def something arg
> end
> 
> ```
> 
> }

I haven’t checked out your library yet.  
Can you set visibility on methods?  
Helper methods don’t have to be public.

Probably I need to see your library first.

I’ll talk to you again.

Sam

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [July 14, 2006, 1:00am UTC](https://www.ruby-forum.com/t/choosing-among-object-class-module-top-level-methods/98343/4 "2006-07-14T01:00:49Z")

</div>

On Fri, 14 Jul 2006, Sam K. wrote:

> > }
> 
> I haven’t checked out your library yet.  
> Can you set visibility on methods?

yes.

-a
