can i shorten up this function ?

N

nephish

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
 
B

Bryan Smith

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
 
N

nephish

Bryan said:
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

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

thanks again
sk
 
J

Joel VanderWerf

nephish said:
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
 
S

Scott

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
 
J

Jim Cochrane

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

--
 
P

Peña, Botp

:fr nephish [mailto:[email protected]]=20
# def get_in_list(status)
# my_list =3D 'x,j,m,jrn,fnk,foo,other'
# my_list_array =3D 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
#=20
# 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
#=20
#=20
#=20
 
N

nephish

Peña said:
:fr nephish [mailto:[email protected]]
# 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top