# Start\_with

**URL:** <https://www.ruby-forum.com/t/start-with/175880>\
**Category:** Ruby\
**Created:** [September 22, 2009, 4:05am UTC](https://www.ruby-forum.com/t/start-with/175880 "2009-09-22T04:05:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rgreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f04885/32.png) [@rgreen](https://www.ruby-forum.com/u/rgreen)\
**Post date:** [September 22, 2009, 4:05am UTC](https://www.ruby-forum.com/t/start-with/175880/1 "2009-09-22T04:05:49Z")

</div>

Can someone explain why this doesn’t work?

class Person  
attr\_accessor :firstName  
P=[]  
def initialize  
P \<\< self  
end  
def self.find\_person(part)  
P.each {|e| if e.firstName.start\_with(part) then return e end}  
end  
end

u=Person.new  
u.firstName=“Ron”  
r=Person.new  
r.firstName=“Bob”

q=Person.find\_person(“Ro”)  
puts q.firstName

---

<div class="post-metadata">

**Author:** ![rgreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f04885/32.png) [@rgreen](https://www.ruby-forum.com/u/rgreen)\
**Post date:** [September 22, 2009, 4:24am UTC](https://www.ruby-forum.com/t/start-with/175880/2 "2009-09-22T04:24:33Z")

</div>

Hi –

On Tue, 22 Sep 2009, Rong wrote:

> P.each {|e| if e.firstName.start\_with(part) then return e end}  
> You mean start\_with? (with question mark).

Also, you’ve got a bug. The #each method returns its receiver. That  
means that if nothing triggers the return inside the block, you’ll get  
back the whole P array. See below…

> end  
> end
> 
> u=Person.new  
> u.firstName=“Ron”  
> r=Person.new  
> r.firstName=“Bob”
> 
> q=Person.find\_person(“Ro”)  
> puts q.firstName

p Person.find\_person(“nobody”)

Output: [#\<Person:0x1ee628 @firstName=“Ron”\>, #\<Person:0x1ee5d8  
@firstName=“Bob”\>]

What you want is:

def self.find\_person(part)  
P.find {|e| e.firstName.start\_with(part) }  
end

which will return nil if nothing is found. (Also, first\_name would be  
more idiomatic than firstName, as a variable name.)

I’ve got reservations about using a constant as a modifiable array  
like you’re doing, but one (or two) thing(s) at a time 🙂

David

---

<div class="post-metadata">

**Author:** ![rgreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f04885/32.png) [@rgreen](https://www.ruby-forum.com/u/rgreen)\
**Post date:** [September 22, 2009, 5:15am UTC](https://www.ruby-forum.com/t/start-with/175880/3 "2009-09-22T05:15:29Z")

</div>

Thanks David. I sent you a tweet.

---

<div class="post-metadata">

**Author:** ![rgreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f04885/32.png) [@rgreen](https://www.ruby-forum.com/u/rgreen)\
**Post date:** [September 22, 2009, 6:14am UTC](https://www.ruby-forum.com/t/start-with/175880/4 "2009-09-22T06:14:01Z")

</div>

I tried changing the array to an instance variable but the code won’t  
work.

---

<div class="post-metadata">

**Author:** ![rgreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f04885/32.png) [@rgreen](https://www.ruby-forum.com/u/rgreen)\
**Post date:** [September 22, 2009, 2:37pm UTC](https://www.ruby-forum.com/t/start-with/175880/5 "2009-09-22T14:37:24Z")

</div>

Hi –

On Tue, 22 Sep 2009, Rong wrote:

> I tried changing the array to an instance variable but the code won’t  
> work.

You’d have to make it an instance variable of the class itself, like  
this:

class Person  
@people = []  
def self.people  
@people  
end

```
 def initialize
   self.class.people << self
 end

```

end

You were very considerate in using a tweet instead of this mailing  
list to point out that, in spite of my saying I had reservations about  
using a constant the way your code did, I had done exactly the same  
thing on page 108 of my book 🙂 So let me try to return the kindness  
by explaining my reservations.

The main issue at least to be aware of is what happens when you  
inherit:

class Person  
P = []  
def initialize  
P \<\< self  
end  
end

class Rubyist \< Person  
end

r = Rubyist.new  
p Person:😛 # =\> [#Rubyist:0x2202a4]

In other words, you end up using the same constant for all the  
subclasses, and thus getting what you might consider false positives  
(depending on exactly what you want to count). This is also why using  
a class variable is a problem in this kind of role: it will also be  
the same variable down the class hierarchy.

The other issue is just the question of using a “constant” for  
something that changes, like this array. I don’t consider that  
to be automatically bad (and it’s very different from re-assigning to  
a constant, which is _really_ non-constant), but it feels perhaps like  
a looser fit than using an instance variable, as in my first example  
above. The instance variable is designed to maintain state on a  
per-object (in this case, per class-object) basis, which a constant  
can do but which isn’t their main or most “natural” role.

David
