# Can i shorten up this function?

**URL:** <https://www.ruby-forum.com/t/can-i-shorten-up-this-function/75594>\
**Category:** Ruby\
**Created:** [November 11, 2006, 7:30am UTC](https://www.ruby-forum.com/t/can-i-shorten-up-this-function/75594 "2006-11-11T07:30:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [November 11, 2006, 7:30am UTC](https://www.ruby-forum.com/t/can-i-shorten-up-this-function/75594/1 "2006-11-11T07:30:41Z")

</div>

hey there, i have a function that checks to see if a string is in a  
comma separated list

it looks like this  
def get\_in\_list(status)  
my\_list = ‘x,j,m,jrn,fnk,foo,other’  
my\_list\_array = my\_list.split(’,’)  
if my\_list\_array.include?(status)  
return true  
else  
return false  
end  
end

i dunno, just thought there might be a prettier way to do this.  
any suggestions ?  
thanks

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [November 11, 2006, 7:44am UTC](https://www.ruby-forum.com/t/can-i-shorten-up-this-function/75594/2 "2006-11-11T07:44:42Z")

</div>

Simply chaining together what you have already provided:

def get\_in\_list(status)  
‘x,j,m,jrn,fnk,foo,other’.split(’,’).include?(status)  
end

Don’t know how pretty a hand-coded, comma-delimited string is, though.  
=)

Cheers,  
Bryan

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [November 11, 2006, 7:50am UTC](https://www.ruby-forum.com/t/can-i-shorten-up-this-function/75594/3 "2006-11-11T07:50:43Z")

</div>

Bryan Smith wrote:

> Bryan
> 
> > ```
> > return true
> > 
> > ```

much better, thanks. the list is an example of a string in a database.  
yeah, ugly.

thanks again  
sk

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [November 11, 2006, 8:46am UTC](https://www.ruby-forum.com/t/can-i-shorten-up-this-function/75594/4 "2006-11-11T08:46:45Z")

</div>

You may want to consider a little refactoring when you are hard-coding  
lists into methods. I also notice that you are returning true/false  
based on a boolean operation. In those cases, returning the result of  
the boolean operation yields the same value, and cuts out an ‘if’  
statment and 4 lines of code:

STATUS\_CODES = %w{x j m jrn fnk foo other}

def is\_valid\_code?(status\_code)  
STATUS\_CODES.include?(status\_code)  
end

is\_valid\_code?(‘jrn’)

-Scott

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [November 11, 2006, 7:54am UTC](https://www.ruby-forum.com/t/can-i-shorten-up-this-function/75594/5 "2006-11-11T07:54:43Z")

</div>

nephish wrote:

> ```
> return false
> end
> 
> ```
> 
> end
> 
> i dunno, just thought there might be a prettier way to do this.  
> any suggestions ?  
> thanks

Not prettier, but might be faster:

def get\_in\_list(status)  
my\_list = ‘x,j,m,jrn,fnk,foo,other’  
/(?:^|,)#{Regexp.quote(status)}(?:,|$)/ === my\_list  
end

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [November 11, 2006, 10:06am UTC](https://www.ruby-forum.com/t/can-i-shorten-up-this-function/75594/6 "2006-11-11T10:06:52Z")

</div>

On 2006-11-11, nephish [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> return false
> end
> 
> ```
> 
> end
> 
> i dunno, just thought there might be a prettier way to do this.  
> any suggestions ?  
> thanks

One possibility:

class HardCoded

```
List = 'x,j,m,jrn,fnk,foo,other'.split(',')

def self.contains(s)
	List.include?(s)
end

```

end

[test:]  
#!/usr/bin/env ruby  
$VERBOSE = true

require ‘hardcoded’

puts HardCoded.contains(‘bother’)  
puts HardCoded.contains(‘other’)

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [November 11, 2006, 10:52am UTC](https://www.ruby-forum.com/t/can-i-shorten-up-this-function/75594/7 "2006-11-11T10:52:56Z")

</div>

:fr nephish [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# def get\_in\_list(status)

# my\_list = ‘x,j,m,jrn,fnk,foo,other’

# my\_list\_array = my\_list.split(‘,’)

a candidate for chaining methods

# if my\_list\_array.include?(status)

# return true

# else

# return false

# end

you are repeating and paraphrasing my\_list\_array.include?(status). lose  
the returns, lose the ifs. this is ruby. r u a c programmer? 😉

nonetheless, this is again a candidate for chaining.

# end

# 

# i dunno, just thought there might be a prettier way to do this.

if you get a solution within a minute, including the typing, then that’s  
pretty 🙂

kind regards -botp

# any suggestions ?

# thanks

# 

# 

#

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [November 11, 2006, 4:11pm UTC](https://www.ruby-forum.com/t/can-i-shorten-up-this-function/75594/8 "2006-11-11T16:11:33Z")

</div>

Peña wrote:

> # return false
> 
> if you get a solution within a minute, including the typing, then that’s pretty 🙂
> 
> kind regards -botp
> 
> # any suggestions ?
> 
> # thanks
> 
> # 
> 
> # 
> 
> #

not a c programmer, just a newbie  
appreciate everyones help, me stuff looks a lot better now.  
-sk
