Newbea question again?

I

Ibrahim Dogru

Hello Ruby Developers,

I have tiny problem about comparison. I have a array and hash. I want to
compare array elements with hash keys.
more details:
this is my hash, I create it from yaml file
my yaml file
template1:
template_of_roster3: template_of_roster2
template_of_roster4: template_of_roster3


@get_config=YAML.load(File.open("#{PATH_IM}/config/converter.yml"))
@get_config == Hash
@my_hash==Hash
@enterprises=@get_config["template1"]

and my array
@my_array=["template_of_roster","template_of_roster2","template_of_roster3"]

my question here is that how can I compare array with hash keys and how
can I can send warning message to user?
@my_array.eql?(@my_hash.key)==false
warn "warning message"

something like this.

thanks..
 
T

Todd Benson

Hello Ruby Developers,

I have tiny problem about comparison. I have a array and hash. I want to
compare array elements with hash keys.
more details:
this is my hash, I create it from yaml file
my yaml file
template1:
template_of_roster3: template_of_roster2
template_of_roster4: template_of_roster3


@get_config=YAML.load(File.open("#{PATH_IM}/config/converter.yml"))
@get_config == Hash
@my_hash==Hash
@enterprises=@get_config["template1"]

and my array
@my_array=["template_of_roster","template_of_roster2","template_of_roster3"]

my question here is that how can I compare array with hash keys and how
can I can send warning message to user?
@my_array.eql?(@my_hash.key)==false
warn "warning message"

something like this.

thanks..

You have a strange structure here. You have values that act as keys.
4 -> 3 and 3 -> 2. But, if it won't be circular, and your hashes are
truly nested, then, here's a cowboy recursive hack...

@b = false
def check h, key
h.each { |k, h| @b ||= h.keys.include?(key) || check(h, key) rescue false; @b}
end

hsh = YAML.load_file("your_file.yml")
puts check(hsh, "template_of_roster")
puts check(hsh, "template_of_roster3")

=> false
=> true

If your hashes are not nested (i.e. you have one level that you are
concerned about), then all you should just use use
h["template1"].keys.include?

hth,
Todd
 
I

Ibrahim Dogru

@b = false
def check h, key
h.each { |k, h| @b ||= h.keys.include?(key) || check(h, key) rescue
false; @b}
end
hsh = YAML.load_file("your_file.yml")
puts check(hsh, "template_of_roster")
puts check(hsh, "template_of_roster3")
=> false
=> true
If your hashes are not nested (i.e. you have one level that you are
concerned about), then all you should just use use
h["template1"].keys.include?


thank you very much..
 
T

Todd Benson

@b = false
def check h, key
h.each { |k, h| @b ||= h.keys.include?(key) || check(h, key) rescue
false; @b}
end
hsh = YAML.load_file("your_file.yml")
puts check(hsh, "template_of_roster")
puts check(hsh, "template_of_roster3")
=> false
=> true
If your hashes are not nested (i.e. you have one level that you are
concerned about), then all you should just use use
h["template1"].keys.include?


thank you very much..

After looking at the documentation, I guess you can simply replace...

#keys.include?

...with...

#has_key?


Todd
 

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

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,296
Latest member
HeikeHolli

Latest Threads

Top