[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

can i shorten up this function ?

shawn bright

11/11/2006 6:27:00 AM

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

7 Answers

Bryan Smith

11/11/2006 6:43:00 AM

0

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

On 11/11/06, nephish <nephish@gmail.com> wrote:
> 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
>
>
>

shawn bright

11/11/2006 6:50:00 AM

0


Bryan Smith wrote:
> 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
>
> On 11/11/06, nephish <nephish@gmail.com> wrote:
> > 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
> >
> >
> >

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

thanks again
sk

Joel VanderWerf

11/11/2006 6:54:00 AM

0

nephish wrote:
> 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

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

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Scott

11/11/2006 7:41:00 AM

0

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

nephish wrote:
> 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

Jim Cochrane

11/11/2006 9:01:00 AM

0

On 2006-11-11, nephish <nephish@gmail.com> wrote:
> 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
>

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')

--

Peña, Botp

11/11/2006 9:51:00 AM

0

:fr nephish [mailto:nephish@gmail.com]
# 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
#
#
#

shawn bright

11/11/2006 3:06:00 PM

0


Pe?a wrote:
> :fr nephish [mailto:nephish@gmail.com]
> # 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
> #
> #
> #

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