Two Variables to One Definition?

T

Tj Superfly

Is it possible to send two different variables to one definition and
then use them separately in that definition?

Here's what I'm trying to do.

==============

#item = url
#species = pet

def exists(item)

existingitems = YAML.load(File.open("./#{species}.yaml"))

existingitems.each {

|exist|

if exist == item

return false

end

}

return true

end

pets.each do |pet|
files.each do |url|
if exists(url)
# here I want to send both the pet and url variable up to definition
puts "Exists"
else
puts "Nope"
end
end
end

=============

I have it all working with just the url, but I need it to know what pet
it's on so it knows what file to open and search in.

Anyone have any suggestions?

P.S. This code isn't an exact copy, I took out a lot of fluff and stuff
to shorten it for the example; it probably won't work if you try and
test the code. I'm just looking to see if there is a way to send both
variables up to the definition and then be able to use them?
 
T

Tj Superfly

Sorry, I just figured it out.

if exists(url, pet)

^ that seems to be working

then in my def i put

def exists(item, species)

:)

If anyone has something better feel free to let me know.
 
R

Robert Klemme

Sorry, I just figured it out.

if exists(url, pet)

^ that seems to be working

then in my def i put

def exists(item, species)

:)

If anyone has something better feel free to let me know.

If you do the same test against the same collection it is cleaner to
just have one parameter and invoke the method twice. But you do not
tell us what logical connection there should be between the two. It
seems you ripped out too much of the original code, for example
"species" is missing and we do not know where it comes from.

Cheers

robert
 
S

Stefan Rusterholz

Since you already got the solution to your original question I'll show
you how you can refactor this code:

Tj said:
#item = url
#species = pet

def exists(item)
existingitems = YAML.load(File.open("./#{species}.yaml"))
existingitems.each {
|exist|
if exist == item
return false
end
}
return true
end

# convention in ruby is to speak in 2nd person, ? is a valid last
character in a method definition
def exist?(item, species)
# depending on your situation you'd better load the yaml files once at
the beginning instead of here
# YAML.load_file is to favor over YAML.load(File.open)
existing_items = YAML.load_file "./#{species}.yaml"
# any? is found in Enumerable, it shortcuts and takes a block to test
items in the collection against
existing_items.any? { |existing| item == existing }
end

Since most likely your collection even responds to #include? properly,
you can use that, so with preloading the files and everything:
def initialize
@species = {}
Species.each { |name|
@species[name] = YAML.load_file "./#{name}.yaml"
}
end

def exist?(item, species)
@species[name].include?(item)
end

You might even want to make seperate classes for your species, then you
can avoid the 2nd argument entirely.

Regards
Stefan
 

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
473,969
Messages
2,570,161
Members
46,709
Latest member
AustinMudi

Latest Threads

Top