# Two Variables to One Definition?

**URL:** <https://www.ruby-forum.com/t/two-variables-to-one-definition/151841>\
**Category:** Ruby\
**Created:** [October 26, 2008, 6:53pm UTC](https://www.ruby-forum.com/t/two-variables-to-one-definition/151841 "2008-10-26T18:53:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![superfly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@superfly](https://www.ruby-forum.com/u/superfly)\
**Post date:** [October 26, 2008, 6:53pm UTC](https://www.ruby-forum.com/t/two-variables-to-one-definition/151841/1 "2008-10-26T18:53:12Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![superfly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@superfly](https://www.ruby-forum.com/u/superfly)\
**Post date:** [October 26, 2008, 7:00pm UTC](https://www.ruby-forum.com/t/two-variables-to-one-definition/151841/2 "2008-10-26T19:00:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![superfly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@superfly](https://www.ruby-forum.com/u/superfly)\
**Post date:** [October 26, 2008, 8:30pm UTC](https://www.ruby-forum.com/t/two-variables-to-one-definition/151841/3 "2008-10-26T20:30:48Z")

</div>

On 26.10.2008 18:59, Tj Superfly wrote:

> 🙂
> 
> 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

---

<div class="post-metadata">

**Author:** ![superfly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@superfly](https://www.ruby-forum.com/u/superfly)\
**Post date:** [October 26, 2008, 10:28pm UTC](https://www.ruby-forum.com/t/two-variables-to-one-definition/151841/4 "2008-10-26T22:28:43Z")

</div>

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

Tj Superfly wrote:

> #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
