[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Can anyone tell me why my code isn't working?

Adam Penny

10/20/2008 10:37:00 PM

Hi there,

For some reason the get_mac_by_printer(host_name) method isn't working.
All the other bits work, but I have a feeling that I'm running into a
problem with variables defined within blocks, but I'm at a loss as to
how to negotiate it. The resulting mac is returning as nilClass and it
definitely isn't nil! Code as follows:

#Class for handling the plist file.
class PlistHash
def initialize(path)
@path=path
@plist_hash=Plist::parse_xml(@path)
end

def get_mac_by_host(host_name)
servers=@plist_hash['servers'].each do |s|
mac=s['mac'] if s['name']==host_name
return mac
end
end

def get_mac_by_printer(printer_name)
printers=@plist_hash['printers']
printers.each do |v|
host=v['host'] if v['name']==printer_name
return host
end
mac=get_mac_by_host(host)
return mac
end

def get_broadcast_ip
broadcast_ip=@plist_hash['broadcastIP']
end
end

Thanks for your help,

Adam
--
Posted via http://www.ruby-....

6 Answers

Adam Penny

10/20/2008 11:39:00 PM

0

Solved it.

I came up with this solution to force the each loops to return a
specific value from the array of hashes. Explicitly returning the value
of the block each block did the trick. Still don't think I've got the
hang of variable scope though. :-(

class PlistHash
def initialize(path)
@plist_hash=Plist::parse_xml(path)
end

def get_mac_by_host(host_name)
servers=@plist_hash['servers'].each do |s|
return s['mac'] if s['name']==host_name
end
end

def get_host_by_printer(printer_name)
printers=@plist_hash['printers']
printers.each do |v|
return v['server'] if v['name']==printer_name
end
end
def get_mac_by_printer(printer_name)
host=self.get_host_by_printer(printer_name)
return self.get_mac_by_host(host)
end

def get_broadcast_ip
broadcast_ip=@plist_hash['broadcastIP']
end
end

--
Posted via http://www.ruby-....

Brian Candler

10/21/2008 8:00:00 AM

0

Adam Penny wrote:
> For some reason the get_mac_by_printer(host_name) method isn't working.
> All the other bits work, but I have a feeling that I'm running into a
> problem with variables defined within blocks, but I'm at a loss as to
> how to negotiate it.

No, nothing to do with variable scoping.

> The resulting mac is returning as nilClass and it
> definitely isn't nil! Code as follows:
...
> def get_mac_by_host(host_name)
> servers=@plist_hash['servers'].each do |s|
> mac=s['mac'] if s['name']==host_name
> return mac
> end
> end

It definitely *is* nil.

Note that your 'each' loop will onlyever run for one iteration, since
'return mac' will always return from the enclosing method
(get_mac_by_host) during the first iteration. So, unless the passed
host_name happens to match the first entry of @plist_hash['servers'],
then the assignment

mac = s['mac'] if s['name']==host_name

will not be executed (because it's qualified by "if ... false condition
..."), so the value of the local variable 'mac' will be nil.

To demonstrate:

if 1 == 2
foo = "oops"
end
puts foo # nil

So your loop is similar to:

def mycode
(1..5).each do |v|
res = v if v == 3
return res
end
end
puts mycode # nil

You could fix this by changin "return res" to "return res if v == 3", or
"return res if res", but more simply

def mycode
(1..5).each do |v|
return v if v == 3
end
end
puts mycode # 3
--
Posted via http://www.ruby-....

Jesús Gabriel y Galán

10/21/2008 8:34:00 AM

0

On Tue, Oct 21, 2008 at 9:59 AM, Brian Candler <b.candler@pobox.com> wrote:

> You could fix this by changin "return res" to "return res if v == 3", or
> "return res if res", but more simply
>
> def mycode
> (1..5).each do |v|
> return v if v == 3
> end
> end
> puts mycode # 3

Also check Enumerable#find:

irb(main):002:0> a = [{:name => "a", :value => 3}, {:name => "b",
:value => 4}, {:name => "c", :value => 10}]
=> [{:value=>3, :name=>"a"}, {:value=>4, :name=>"b"}, {:value=>10, :name=>"c"}]
irb(main):003:0> a.find {|s| s[:name] == "b"}
=> {:value=>4, :name=>"b"}

So, you method could be done as:

def get_mac_by_host(host_name)
s = @plist_hash['servers'].find {|s| s['name'] == host_name}
return s['mac'] if s
nil
end

(not tested). Hope this helps,

Jesus.

Brian Candler

10/21/2008 8:47:00 AM

0

Just to make the semantics of 'return' clearer:

def foo
1
return 2
raise "Never get here"
3
end

puts foo # 2

return doesn't cause store a value for returning later; it causes the
method to return immediately, returning that value.
--
Posted via http://www.ruby-....

r

4/20/2014 10:40:00 PM

0

In article <ure8l9t21710dmgdibsfg6243l8c69sqhn@4ax.com>, PATRICK
<pbarker001@woh.rr.com> wrote:

> On Sun, 20 Apr 2014 08:53:54 -0400, NoBody <NoBody@nowhere.com> wrote:
>
> >On Sat, 19 Apr 2014 05:51:50 -0700, r@somis.org (?RLMeasures) wrote:
> >
> >>In article <ndo4l9h1e60dmc5jipbk4je62lpqovbkk0@4ax.com>, NoBody
> >><NoBody@nowhere.com> wrote:
> >>
> >>> On Fri, 18 Apr 2014 07:55:45 -0700, r@somis.org (?RLMeasures) wrote:
> >>>
> >>> >In article <0g12l956n19qsgoghntpihtursk1e48cnv@4ax.com>, NoBody
> >>> ><NoBody@nowhere.com> wrote:
> >>> >
> >>> >> On Thu, 17 Apr 2014 06:43:33 -0700, r@somis.org (?RLMeasures) wrote:
> >>> >>
> >>> >> >In article <agbvk993q1ch366bh8n1r5ej76d9hp1h9g@4ax.com>, NoBody
> >>> >> ><NoBody@nowhere.com> wrote:
> >>> >> >
> >>> >> >> On Wed, 16 Apr 2014 13:38:02 -0700, r@somis.org (?RLMeasures) wrote:
> >>> >> >>
> >>> >> >> >In article <lvnsk95cgfj7d4rkm6t3piv34sft520718@4ax.com>, NoBody
> >>> >> >> ><NoBody@nowhere.com> wrote:
> >>> >> >> >
> >>> >> >> >> On Tue, 15 Apr 2014 05:41:09 -0700, r@somis.org
(?RLMeasures) wrote:
> >>> >> >> >>
> >>> >> >> >> >In article <353qk9lv483aeogtr1gcs4fig2u32ckujo@4ax.com>, NoBody
> >>> >> >> >> ><NoBody@nowhere.com> wrote:
> >>> >> >> >> >
> >>> >> >> >> >> On Mon, 14 Apr 2014 13:31:26 -0700, r@somis.org
> >>(?RLMeasures) wrote:
> >>> >> >> >> >>
> >>> >> >> >> >> >In article
<4hfnk9tgmpibf1hbrot5k5033obacl4umk@4ax.com>, NoBody
> >>> >> >> >> >> ><NoBody@nowhere.com> wrote:
> >>> >> >> >> >> >
> >>> >> >> >> >> >> On Sun, 13 Apr 2014 18:37:36 -0700, r@somis.org
> >>> >(?RLMeasures) wrote:
> >>> >> >> >> >> >>
> >>> >> >> >> >> >> >In article <ngilk9drc60g7r47dtlm9rkv94j0m863v2@4ax.com>,
> >>PATRICK
> >>> >> >> >> >> >> ><pbarker001@woh.rr.com> wrote:
> >>> >> >> >> >> >> >
> >>> >> >> >> >> >> >> On Sun, 13 Apr 2014 09:37:49 -0700, r@somis.org
> >>> >> >(?RLMeasures) wrote:
> >>> >> >> >> >> >> >>
> >>> >> >> >> >> >> >> >In article
<m43lk9h82q7b0ur9ftcu8hd2beer7l0e3h@4ax.com>,
> >>> >NoBody
> >>> >> >> >> >> >> >> ><NoBody@nowhere.com> wrote:
> >>> >> >> >> >> >> >> >
> >>> >> >> >> >> >> >> >> On Sat, 12 Apr 2014 19:46:22 -0700, r@somis.org
> >>> >> >> >(?RLMeasures) wrote:
> >>> >> >> >> >> >> >> >>
> >>> >> >> >> >> >> >> >> >In article <lic0i2$9nt$1@news.xmission.com>,
> >>> >> >> >> >gazelle@shell.xmission.com
> >>> >> >> >> >> >> >> >> >(Kenny McCormack) wrote:
> >>> >> >> >> >> >> >> >> >
> >>> >> >> >> >> >> >> >> >> In article <120420141214553292%nope@noway.com>, A
> >>> >Friend <A
> >>> >> >> >> >> >> >Friend> wrote:
> >>> >> >> >> >> >> >> >> >> >In article
<libftf$u6t$2@news.xmission.com>, Kenny
> >>> >> >McCormack
> >>> >> >> >> >> >> >> >> >> ><gazelle@shell.xmission.com> wrote:
> >>> >> >> >> >> >> >> >> >> >
> >>> >> >> >> >> >> >> >> >> >> In article
> >>> ><m4gik9lh4k1m4i6i67i4569ondl8nn4m4l@4ax.com>,
> >>> >> >> >> >> >> >> >> >> >> NoBody <NoBody@nowhere.com> wrote:
> >>> >> >> >> >> >> >> >> >> >> ...
> >>> >> >> >> >> >> >> >> >> >> >You continue to amuse me. Thanks!
> >>> >> >> >> >> >> >> >> >> >>
> >>> >> >> >> >> >> >> >> >> >> And vice versa. I.e., you haven't made
it into my
> >>> >> >> >> >killfile yet...
> >>> >> >> >> >> >> >> >> >> >
> >>> >> >> >> >> >> >> >> >> >
> >>> >> >> >> >> >> >> >> >> >He's made it into mine. He does nothing but spew
> >>> >brainless
> >>> >> >> >> >> >bullshit,
> >>> >> >> >> >> >> >> >> >> >and when you finally get tired of him and stop
> >>> >responding,
> >>> >> >> >> >he cries
> >>> >> >> >> >> >> >> >> >> >"Silence! Just as I expected!" There's no point
> >>> >in dealing
> >>> >> >> >> >> >with him.
> >>> >> >> >> >> >> >> >> >>
> >>> >> >> >> >> >> >> >> >> Oh, agreed. But it still makes for amusing
reading.
> >>> >> >> >> >> >> >> >> >>
> >>> >> >> >> >> >> >> >> >> I leave to RL to actually respond to him.
He seems to
> >>> >> >> >enjoy his
> >>> >> >> >> >> >> >assigned
> >>> >> >> >> >> >> >> >> >> task.
> >>> >> >> >> >> >> >> >> >
> >>> >> >> >> >> >> >> >> >? History deniers are a source of enjoyment.
> >>> >> >> >> >> >> >> >>
> >>> >> >> >> >> >> >> >> What history is that? Are you referring to the
> >>encyclical
> >>> >> >> >you claim
> >>> >> >> >> >> >> >> >> exist but can't show that it does?
> >>> >> >> >> >> >> >> >>
> >>> >> >> >> >> >> >> >> What is truly funny, is that that you guys seem to be
> >>> >> >> >high-fiving RL
> >>> >> >> >> >> >> >> >> for lying outright.
> >>> >> >> >> >> >> >> >
> >>> >> >> >> >> >> >> >? that's because they know who shoots blanks and who
> >>> >doesn't.
> >>> >> >> >> >. . That
> >>> >> >> >> >> >> >> >in 1866 Pius IX would assume it was impossible for a
> >>> >genuine RC
> >>> >> >> >> >priest to
> >>> >> >> >> >> >> >> >sodomize RC altar- boys seems reasonable because at
> >>> >first I had
> >>> >> >> >> >mistakenly
> >>> >> >> >> >> >> >> >assumed the same thing.
> >>> >> >> >> >> >> >>
> >>> >> >> >> >> >> >> And then you FURTHER assume that he wrote a "secret"
> >>> >> >encyclical that
> >>> >> >> >> >> >> >> only a few of you know about. You really are special,
> >>> >aren't you?
> >>> >> >> >> >> >> >
> >>> >> >> >> >> >> >? hardly PB. I'm just a guy who watched the HBO
> >>documentary and
> >>> >> >> >realized
> >>> >> >> >> >> >>
> >>> >> >> >> >> >> That documentary for which you provided a dead link?
> >>BWWAAAAAA!
> >>> >> >> >> >> >
> >>> >> >> >> >> >? zzz
> >>> >> >> >> >>
> >>> >> >> >> >> Your surrender is so noted.
> >>> >> >> >> >
> >>> >> >> >> >? You seem to have already forgotten that I watched the HBO
> >>> >documentary.
> >>> >> >> >>
> >>> >> >> >> The documentary you have failed to show exists? ??? Good one.
> >>> >> >> >> If it exists, from where did they get their information?
> >>> >> >> >
> >>> >> >> >? HBO knows. I do not.
> >>> >> >>
> >>> >> >> Show the documentary existed. Broken links are not proof. Provide
> >>> >> >> the name and IMDB reference.
> >>> >> >
> >>> >> >? zzz
> >>> >>
> >>> >> Translation: RL is a liar.
> >>> >>
> >>> >> >>
> >>> >> >> >
> >>> >> >> >>Do you
> >>> >> >> >> believe everything you see on TV?
> >>> >> >> >>
> >>> >> >> >? chortle. Get serious.
> >>> >> >>
> >>> >> >> Right, you only believe what fits into your narrow, bigotted view of
> >>> >> >> the world.
> >>> >> >
> >>> >> >? my view is not influenced by the Church of Rome. Your's is.
> >>> >>
> >>> >> Your "view" is not based on any form of facts. You decide what your
> >>> >> reality is. One only needs your posts about encyclicals that never
> >>> >> existed.
> >>> >
> >>> >? "It ain't what you don't know that gets you into trouble. It's
what you
> >>> >know for sure that just ain't so."
> >>> > ? Mark Twain
> >>>
> >>> There goes that irony bell again as you avoid your lie.
> >>
> >>? SOS
> >
> >That's what you've been producing, yes.
>
> I think he may be begging for mercy....

? hardly. History deniers are serendipitous delight for critics of
"God's Holy Church".

John Doe

4/21/2014 11:10:00 AM

0

On Sun, 20 Apr 2014 07:13:37 -0700, r@somis.org (?RLMeasures) wrote:

>In article <kqg7l9l59absketacle51hm42il6s201gr@4ax.com>, NoBody
><NoBody@nowhere.com> wrote:
>
>> On Sat, 19 Apr 2014 05:51:50 -0700, r@somis.org (?RLMeasures) wrote:
>>
>> >In article <ndo4l9h1e60dmc5jipbk4je62lpqovbkk0@4ax.com>, NoBody
>> ><NoBody@nowhere.com> wrote:
>> >
>> >> On Fri, 18 Apr 2014 07:55:45 -0700, r@somis.org (?RLMeasures) wrote:
>> >>
>> >> >In article <0g12l956n19qsgoghntpihtursk1e48cnv@4ax.com>, NoBody
>> >> ><NoBody@nowhere.com> wrote:
>> >> >
>> >> >> On Thu, 17 Apr 2014 06:43:33 -0700, r@somis.org (?RLMeasures) wrote:
>> >> >>
>> >> >> >In article <agbvk993q1ch366bh8n1r5ej76d9hp1h9g@4ax.com>, NoBody
>> >> >> ><NoBody@nowhere.com> wrote:
>> >> >> >
>> >> >> >> On Wed, 16 Apr 2014 13:38:02 -0700, r@somis.org (?RLMeasures) wrote:
>> >> >> >>
>> >> >> >> >In article <lvnsk95cgfj7d4rkm6t3piv34sft520718@4ax.com>, NoBody
>> >> >> >> ><NoBody@nowhere.com> wrote:
>> >> >> >> >
>> >> >> >> >> On Tue, 15 Apr 2014 05:41:09 -0700, r@somis.org
>(?RLMeasures) wrote:
>> >> >> >> >>
>> >> >> >> >> >In article <353qk9lv483aeogtr1gcs4fig2u32ckujo@4ax.com>, NoBody
>> >> >> >> >> ><NoBody@nowhere.com> wrote:
>> >> >> >> >> >
>> >> >> >> >> >> On Mon, 14 Apr 2014 13:31:26 -0700, r@somis.org
>> >(?RLMeasures) wrote:
>> >> >> >> >> >>
>> >> >> >> >> >> >In article <4hfnk9tgmpibf1hbrot5k5033obacl4umk@4ax.com>,
>NoBody
>> >> >> >> >> >> ><NoBody@nowhere.com> wrote:
>> >> >> >> >> >> >
>> >> >> >> >> >> >> On Sun, 13 Apr 2014 18:37:36 -0700, r@somis.org
>> >> >(?RLMeasures) wrote:
>> >> >> >> >> >> >>
>> >> >> >> >> >> >> >In article <ngilk9drc60g7r47dtlm9rkv94j0m863v2@4ax.com>,
>> >PATRICK
>> >> >> >> >> >> >> ><pbarker001@woh.rr.com> wrote:
>> >> >> >> >> >> >> >
>> >> >> >> >> >> >> >> On Sun, 13 Apr 2014 09:37:49 -0700, r@somis.org
>> >> >> >(?RLMeasures) wrote:
>> >> >> >> >> >> >> >>
>> >> >> >> >> >> >> >> >In article <m43lk9h82q7b0ur9ftcu8hd2beer7l0e3h@4ax.com>,
>> >> >NoBody
>> >> >> >> >> >> >> >> ><NoBody@nowhere.com> wrote:
>> >> >> >> >> >> >> >> >
>> >> >> >> >> >> >> >> >> On Sat, 12 Apr 2014 19:46:22 -0700, r@somis.org
>> >> >> >> >(?RLMeasures) wrote:
>> >> >> >> >> >> >> >> >>
>> >> >> >> >> >> >> >> >> >In article <lic0i2$9nt$1@news.xmission.com>,
>> >> >> >> >> >gazelle@shell.xmission.com
>> >> >> >> >> >> >> >> >> >(Kenny McCormack) wrote:
>> >> >> >> >> >> >> >> >> >
>> >> >> >> >> >> >> >> >> >> In article <120420141214553292%nope@noway.com>, A
>> >> >Friend <A
>> >> >> >> >> >> >> >Friend> wrote:
>> >> >> >> >> >> >> >> >> >> >In article <libftf$u6t$2@news.xmission.com>, Kenny
>> >> >> >McCormack
>> >> >> >> >> >> >> >> >> >> ><gazelle@shell.xmission.com> wrote:
>> >> >> >> >> >> >> >> >> >> >
>> >> >> >> >> >> >> >> >> >> >> In article
>> >> ><m4gik9lh4k1m4i6i67i4569ondl8nn4m4l@4ax.com>,
>> >> >> >> >> >> >> >> >> >> >> NoBody <NoBody@nowhere.com> wrote:
>> >> >> >> >> >> >> >> >> >> >> ...
>> >> >> >> >> >> >> >> >> >> >> >You continue to amuse me. Thanks!
>> >> >> >> >> >> >> >> >> >> >>
>> >> >> >> >> >> >> >> >> >> >> And vice versa. I.e., you haven't made it
>into my
>> >> >> >> >> >killfile yet...
>> >> >> >> >> >> >> >> >> >> >
>> >> >> >> >> >> >> >> >> >> >
>> >> >> >> >> >> >> >> >> >> >He's made it into mine. He does nothing but spew
>> >> >brainless
>> >> >> >> >> >> >bullshit,
>> >> >> >> >> >> >> >> >> >> >and when you finally get tired of him and stop
>> >> >responding,
>> >> >> >> >> >he cries
>> >> >> >> >> >> >> >> >> >> >"Silence! Just as I expected!" There's no point
>> >> >in dealing
>> >> >> >> >> >> >with him.
>> >> >> >> >> >> >> >> >> >>
>> >> >> >> >> >> >> >> >> >> Oh, agreed. But it still makes for amusing
>reading.
>> >> >> >> >> >> >> >> >> >>
>> >> >> >> >> >> >> >> >> >> I leave to RL to actually respond to him. He
>seems to
>> >> >> >> >enjoy his
>> >> >> >> >> >> >> >assigned
>> >> >> >> >> >> >> >> >> >> task.
>> >> >> >> >> >> >> >> >> >
>> >> >> >> >> >> >> >> >> >? History deniers are a source of enjoyment.
>> >> >> >> >> >> >> >> >>
>> >> >> >> >> >> >> >> >> What history is that? Are you referring to the
>> >encyclical
>> >> >> >> >you claim
>> >> >> >> >> >> >> >> >> exist but can't show that it does?
>> >> >> >> >> >> >> >> >>
>> >> >> >> >> >> >> >> >> What is truly funny, is that that you guys seem to be
>> >> >> >> >high-fiving RL
>> >> >> >> >> >> >> >> >> for lying outright.
>> >> >> >> >> >> >> >> >
>> >> >> >> >> >> >> >> >? that's because they know who shoots blanks and who
>> >> >doesn't.
>> >> >> >> >> >. . That
>> >> >> >> >> >> >> >> >in 1866 Pius IX would assume it was impossible for a
>> >> >genuine RC
>> >> >> >> >> >priest to
>> >> >> >> >> >> >> >> >sodomize RC altar- boys seems reasonable because at
>> >> >first I had
>> >> >> >> >> >mistakenly
>> >> >> >> >> >> >> >> >assumed the same thing.
>> >> >> >> >> >> >> >>
>> >> >> >> >> >> >> >> And then you FURTHER assume that he wrote a "secret"
>> >> >> >encyclical that
>> >> >> >> >> >> >> >> only a few of you know about. You really are special,
>> >> >aren't you?
>> >> >> >> >> >> >> >
>> >> >> >> >> >> >> >? hardly PB. I'm just a guy who watched the HBO
>> >documentary and
>> >> >> >> >realized
>> >> >> >> >> >> >>
>> >> >> >> >> >> >> That documentary for which you provided a dead link?
>> >BWWAAAAAA!
>> >> >> >> >> >> >
>> >> >> >> >> >> >? zzz
>> >> >> >> >> >>
>> >> >> >> >> >> Your surrender is so noted.
>> >> >> >> >> >
>> >> >> >> >> >? You seem to have already forgotten that I watched the HBO
>> >> >documentary.
>> >> >> >> >>
>> >> >> >> >> The documentary you have failed to show exists? ??? Good one.
>> >> >> >> >> If it exists, from where did they get their information?
>> >> >> >> >
>> >> >> >> >? HBO knows. I do not.
>> >> >> >>
>> >> >> >> Show the documentary existed. Broken links are not proof. Provide
>> >> >> >> the name and IMDB reference.
>> >> >> >
>> >> >> >? zzz
>> >> >>
>> >> >> Translation: RL is a liar.
>> >> >>
>> >> >> >>
>> >> >> >> >
>> >> >> >> >>Do you
>> >> >> >> >> believe everything you see on TV?
>> >> >> >> >>
>> >> >> >> >? chortle. Get serious.
>> >> >> >>
>> >> >> >> Right, you only believe what fits into your narrow, bigotted view of
>> >> >> >> the world.
>> >> >> >
>> >> >> >? my view is not influenced by the Church of Rome. Your's is.
>> >> >>
>> >> >> Your "view" is not based on any form of facts. You decide what your
>> >> >> reality is. One only needs your posts about encyclicals that never
>> >> >> existed.
>> >> >
>> >> >? "It ain't what you don't know that gets you into trouble. It's what you
>> >> >know for sure that just ain't so."
>> >> > ? Mark Twain
>> >>
>> >> There goes that irony bell again as you avoid your lie.
>> >
>> >? SOS
>>
>> That's what you've been producing, yes.
>
>? zzz

Your surrender is duly noted.