# Question on setting up a class and searching on objects

**URL:** <https://www.ruby-forum.com/t/question-on-setting-up-a-class-and-searching-on-objects/102382>\
**Category:** Ruby\
**Created:** [August 15, 2006, 2:16pm UTC](https://www.ruby-forum.com/t/question-on-setting-up-a-class-and-searching-on-objects/102382 "2006-08-15T14:16:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [August 15, 2006, 2:16pm UTC](https://www.ruby-forum.com/t/question-on-setting-up-a-class-and-searching-on-objects/102382/1 "2006-08-15T14:16:39Z")

</div>

I’m working from Ruby for Rails book but want to try and just  
replicate some of the examples in Ruby alone.  
One method in particular is called whole\_name and would give the user  
the ability to search a list by first\_name, middle\_name, or last\_name.

So I went ahead and set up my class and objects, but have a few  
questions (below code)  
class Composer

def listcomp (first\_name, middle\_name, last\_name)  
f = first\_name  
m = middle\_name  
l = last\_name  
end

def whole\_name  
first\_name + " " +  
(if middle\_name then middle\_name + " " else “” end) +  
last\_name  
end  
end

comp1 = Composer.new  
comp2 = Composer.new  
comp3 = Composer.new  
comp4 = Composer.new  
comp5 = Composer.new

comp1.listcomp(“ludwig”,“van”,“bethoven”)  
comp2.listcomp(“wolfgang”,“amadeus”,“mozart”)  
comp3.listcomp(“franz”, “joseph”, “hayden”)  
comp4.listcomp(“George”, “frederic”, “Handel”)  
comp5.listcomp(“Johann”, “sebastian”, “bach”)

Question - I’m having a problem calling to the class method  
whole\_name. The method is defined without any arguments so how could  
it be called ?

Question - Did I set up the class correctly , defining the new object  
outside of the class definition ? And would it be better to create an  
array (multidimensional) for the composers instead of initializing  
each one seperately ? (If this question makes sense)

Apologies if this question is sort of lame.

TIA  
Stuart

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [August 15, 2006, 2:16pm UTC](https://www.ruby-forum.com/t/question-on-setting-up-a-class-and-searching-on-objects/102382/2 "2006-08-15T14:16:44Z")

</div>

On Aug 11, 2006, at 7:47 AM, Dark A. wrote:

> I’m working from Ruby for Rails book but want to try and just  
> replicate some of the examples in Ruby alone.  
> One method in particular is called whole\_name and would give the user  
> the ability to search a list by first\_name, middle\_name, or last\_name.

If you want to make a method to search the the individual composers,  
you will need to get them into some data structure (like an Array)  
where you can scan through them.

> class Composer
> 
> def listcomp (first\_name, middle\_name, last\_name)  
> f = first\_name  
> m = middle\_name  
> l = last\_name  
> end

What are you intending this method to do? Currently, it doesn’t do  
anything meaningful:

1. first\_name, middle\_name, and last\_name are accepted as arguments
2. Each of those is assigned to another local variable
3. All six local variables are discarded as the method exits

> def whole\_name  
> first\_name + " " +  
> (if middle\_name then middle\_name + " " else “” end) +  
> last\_name  
> end  
> end

This method doesn’t work because it uses local variables that don’t  
exist in this method. I believe this is your hang-up.

Inside a class, you keep data around by putting it into instance  
variables (those start with an @ symbol). All methods of the class  
can access the instance variables for the current object.

See if this helps explain things:

> > class Name  
> > def initialize(first, last, middle = nil)  
> > # assign these local variables to instance variables…  
> > ?\> @first = first
> 
> > ```
> > @last = last
> > @middle = middle
> > 
> > ```
> > 
> > end

?\> # create methods to read those instance variables…  
?\> attr\_reader :first, :last, :middle

> >

?\> def full

> > ```
> > [@first, @middle, @last].join(" ").squeeze(" ")
> > 
> > ```
> > 
> > end  
> > end  
> > =\> nil
> 
> > james = Name.new(“James”, “Gray”, “Edward”)  
> > =\> #\<Name:0x31a5e0 @last=“Gray”, @first=“James”, @middle=“Edward”\>
> 
> > james.last  
> > =\> “Gray”
> 
> > james.full  
> > =\> “James Edward G.”
> 
> > dana = Name.new(“Dana”, “Gray”)  
> > =\> #\<Name:0x314c44 @last=“Gray”, @first=“Dana”, @middle=nil\>
> 
> > dana.middle  
> > =\> nil
> 
> > dana.full  
> > =\> “Dana Gray”

> comp5.listcomp(“Johann”, “sebastian”, “bach”)  
> Here’s another idea:

> > composers = [%w[ludwig van beethoven],  
> > ?\> %w[wolfgan amadeus mozart],  
> > ?\> %w[franz joseph hayden],  
> > ?\> %w[george frederic handel],  
> > ?\> %w[johann sebastian bach] ].map do |f, m, l|  
> > ?\> Name.new(f, m, l)
> 
> > end  
> > =\> [#\<Name:0x356040 @last=“van”, @first=“ludwig”,  
> > @middle=“beethoven”\>, #\<Name:0x35602c @last=“amadeus”,  
> > @first=“wolfgan”, @middle=“mozart”\>, #\<Name:0x356018 @last=“joseph”,  
> > @first=“franz”, @middle=“hayden”\>, #\<Name:0x356004 @last=“frederic”,  
> > @first=“george”, @middle=“handel”\>, #\<Name:0x355ff0  
> > @last=“sebastian”, @first=“johann”, @middle=“bach”\>]

> Question - I’m having a problem calling to the class method  
> whole\_name. The method is defined without any arguments so how could  
> it be called ?

whole\_name() is not a class method, it is an instance method. You  
know this because it is called on instances of the class, not the  
class itself (new() is a class method).

I hope my examples above cleared up calling the methods.

> Question - Did I set up the class correctly , defining the new object  
> outside of the class definition ? And would it be better to create an  
> array (multidimensional) for the composers instead of initializing  
> each one seperately ? (If this question makes sense)

See above.

Hope that helps.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [August 15, 2006, 2:16pm UTC](https://www.ruby-forum.com/t/question-on-setting-up-a-class-and-searching-on-objects/102382/3 "2006-08-15T14:16:47Z")

</div>

James,  
Thank you for the explanation and example. Ran through you code in  
irb so the next step is implementing my method for a search function.  
Hopefully I will only be back to say I’ve figured it out.

No help for now though.

Stuart
