[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Ruby 1.8.6 - normal behavior ?

aldric[removeme]

12/16/2008 8:26:00 PM

I don't know enough to really test this but I came across a funny
behavior - example code requires Net::LDAP.

#
# Prepare and start loop iterating through data in a Windows AD.
# 'entry' is the name of iterator.

#Apparently, the simple fact of checking this keeps Ruby from raising
#a NoMethodError when those entries don't exist
puts "" if entry[:lastlogon].nil?
puts "" if entry[:operatingsystemservicepack].nil?
puts "" if entry[:operatingsystem].nil?

By typing those lines, I get no related output, but I also avoid
crashes. It's pretty neat but it feels a little hackish. Is this
normal/expected behavior?

--Aldric
9 Answers

ThoML

12/16/2008 9:32:00 PM

0

> #Apparently, the simple fact of checking this keeps Ruby from raising
> #a NoMethodError when those entries don't exist

Entry is a hash I presume?

a = {:a => 1, :b => 2}

a[:a]
=> 1
a[:x]
nil

aldric[removeme]

12/16/2008 9:45:00 PM

0

Tom Link wrote:
>> #Apparently, the simple fact of checking this keeps Ruby from raising
>> #a NoMethodError when those entries don't exist
>>
>
> Entry is a hash I presume?
>
> a = {:a => 1, :b => 2}
>
> a[:a]
> => 1
> a[:x]
> nil
>
Entry is a very big hash and I use those values later - if I don't run
those three lines, I'll get a crash, but if I run those three lines,
using those values is fine even if they don't exist... Oh -- is that
because it -creates- the key-value pair when I look at it ?

Michael Libby

12/16/2008 10:37:00 PM

0

On Tue, Dec 16, 2008 at 3:39 PM, Aldric Giacomoni
<"aldric[remove]"@trevoke.net> wrote:
> Tom Link wrote:

> Entry is a very big hash and I use those values later - if I don't run those
> three lines, I'll get a crash, but if I run those three lines, using those
> values is fine even if they don't exist... Oh -- is that because it
> -creates- the key-value pair when I look at it ?

If the code you posted is really the only relevant code and entry is a
Hash, then no.

irb(main):003:0> my_big_hash = {}
=> {}

Empty.

irb(main):004:0> puts "x" if my_big_hash[:foo].nil?
x
=> nil

As expected.

irb(main):005:0> my_big_hash
=> {}

Still empty.

irb(main):006:0> my_big_hash[:foo].any_method
NoMethodError: undefined method `any_method' for nil:NilClass
from (irb):6
from :0

Still getting an error.

So either your entry object is not a basic Hash, or something else is happening.

-Michael


--
Michael C. Libby
www.mikelibby.com

ThoML

12/16/2008 10:53:00 PM

0

> I'll get a crash

Crash as in fatal error dump crash?

> is that because it -creates- the key-value pair when I look at it ?

It's possible to define a hash that way:

a = Hash.new {|h,k| h[k] = nil}
a[:x]
=> nil
a
=> {:x => nil}

You could check with

p entry.default_proc

or

p entry.has_key?(:foo)
p entry[:foo]
p entry.has_key?(:foo)

aldric[removeme]

12/18/2008 1:31:00 PM

0

Just in case.. Here is the whole loop. Please forgive the lack of
elegance, I am a beginner at Ruby.

ldap.search( :base => treebase, :filter => filter ) do |entry|
total_count += 1
threads << Thread.new(entry.name) do |name|
name, address, ping_result = entry.name.to_s, "", ""
begin
address = dns.getaddress(name + "." + domain).to_s
rescue Resolv::ResolvError
address = "No entry"
end

#Apparently, the simple fact of checking this keeps Ruby from raising
#A NoMethodError when those entries don't exist
puts "" if entry[:lastlogon].nil?
puts "" if entry[:operatingsystemservicepack].nil?
puts "" if entry[:operatingsystem].nil?

last_logon = get_last_logon(entry.lastlogon.to_s)
## 'bad' programming - taking known output from AD and formatting
that into a more readable form.
where_in_ad = ""
entry.dn.to_s.split(",").reverse_each do |piece_of_puzzle|
where_in_ad += piece_of_puzzle[3..-1] + '/' if
piece_of_puzzle[0..2] != "DC="
end

## Finding the subnet from an IPv4
subnet = address.split(".")[2].to_i
## Deciding which worksheet this entry belongs to.
loc.include?(subnet) ? location = loc[subnet] : location = 'Others'

begin
if address != 'No entry'
ping_result = Ping.pingecho(name, 5, 80)
else ping_result = 'false'
end
rescue Timeout::Error
ping_result = 'false'
end

sheet = book.worksheet(location)
row = sheet.last_row_index + 1
sheet.row(row).default_format=(bold) if (address != 'No entry' &&
ping_result == 'false')
sheet.row(row).push(name, address, where_in_ad,
entry.operatingsystem.to_s, entry.operatingsystemservicepack.to_s,
last_logon, ping_result)
end
end
threads.each{ |t| t.join}


Does this help a little?
Thanks,
-Aldric

jg

5/17/2011 11:34:00 AM

0

Oy rool out a carbon tax wrote:
> jg wrote:
>> Oy rool out a carbon tax wrote:
>>> jg wrote:
>> ............
>>>>> Oh. I dunno. If you can do something as insane as getting the EPA to
>>>>> declare a natural, nay vital, component of mother nature's
>>>>> atmosphere, a
>>>>> "dangerous pollutant" you can probably achieve anything. When you base
>>>>> that decision on the sage advice of one James Hansen (America's Tim
>>>>> Flannery) who predicted that Manhattan should be underwater by now,
>>>>> then
>>>>> I guess you could get any legislation passed.
>>>>>
>>>> Put whatever labels you like on things, the same volume of gas comes out
>>>> the exhaust pipe for a tank of fuel.
>>> True. Australia contributes 1.28% of it globally and soaks up more than
>>> it produces. We should be paid by the world council of CO2 for taking
>>> care of this dangerous pollutant for the other nations.
>>>
>> Our absorption might be minimal compared with CO2 production too, so any
>> payout would be commensurate with value to the purchaser. But FGS will
>> someone mention the mouse in the room full of elephants?
> FGS?
>

For god's sake.
http://www.internetslang.c...


>> ...........
>>>> The EU takes AGW seriously.
>>> It structures R&D sensibly around alternative energies, with or without
>>> believing GW or the hypothetical A in front of it. The money, if
>>> appropriately channeled, results in genuinely useful innovation,
>>> regardless of what people speculate from the temperature record.
>>>
>> Hey, that's what I think should happen. 'Belief' is secondary.
>
> The value being that if belief turns out to be wrong, you've still made
> a significant improvement/ contribution in the spirit of <gag> "moving
> forward".
>
>

Oy rool out a carbon tax

5/17/2011 1:48:00 PM

0

jg wrote:
> Oy rool out a carbon tax wrote:
>> jg wrote:
>>> Oy rool out a carbon tax wrote:
>>>> jg wrote:
>>> ............
>>>>>> Oh. I dunno. If you can do something as insane as getting the EPA to
>>>>>> declare a natural, nay vital, component of mother nature's
>>>>>> atmosphere, a
>>>>>> "dangerous pollutant" you can probably achieve anything. When you
>>>>>> base
>>>>>> that decision on the sage advice of one James Hansen (America's Tim
>>>>>> Flannery) who predicted that Manhattan should be underwater by now,
>>>>>> then
>>>>>> I guess you could get any legislation passed.
>>>>>>
>>>>> Put whatever labels you like on things, the same volume of gas
>>>>> comes out
>>>>> the exhaust pipe for a tank of fuel.
>>>> True. Australia contributes 1.28% of it globally and soaks up more than
>>>> it produces. We should be paid by the world council of CO2 for taking
>>>> care of this dangerous pollutant for the other nations.
>>>>
>>> Our absorption might be minimal compared with CO2 production too, so any
>>> payout would be commensurate with value to the purchaser. But FGS will
>>> someone mention the mouse in the room full of elephants?
>> FGS?
>>
>
> For god's sake.
> http://www.internetslang.c...
OK. So you're saying Australia is the mouse?
>
>
>>> ...........
>>>>> The EU takes AGW seriously.
>>>> It structures R&D sensibly around alternative energies, with or without
>>>> believing GW or the hypothetical A in front of it. The money, if
>>>> appropriately channeled, results in genuinely useful innovation,
>>>> regardless of what people speculate from the temperature record.
>>>>
>>> Hey, that's what I think should happen. 'Belief' is secondary.
>>
>> The value being that if belief turns out to be wrong, you've still made
>> a significant improvement/ contribution in the spirit of <gag> "moving
>> forward".
>>
>>


--
"If we cut emissions today, global temperatures are not likely to drop
for about a thousand years. "
-- Tim (it ain't a gonna rain no more) Flannery
- Australian Climate Commissar

jg

5/17/2011 8:20:00 PM

0

Oy rool out a carbon tax wrote:
> jg wrote:
>> Oy rool out a carbon tax wrote:
>>> jg wrote:
>>>> Oy rool out a carbon tax wrote:
>>>>> jg wrote:
>>>> ............
>>>>>>> Oh. I dunno. If you can do something as insane as getting the EPA to
>>>>>>> declare a natural, nay vital, component of mother nature's
>>>>>>> atmosphere, a
>>>>>>> "dangerous pollutant" you can probably achieve anything. When you
>>>>>>> base
>>>>>>> that decision on the sage advice of one James Hansen (America's Tim
>>>>>>> Flannery) who predicted that Manhattan should be underwater by now,
>>>>>>> then
>>>>>>> I guess you could get any legislation passed.
>>>>>>>
>>>>>> Put whatever labels you like on things, the same volume of gas
>>>>>> comes out
>>>>>> the exhaust pipe for a tank of fuel.
>>>>> True. Australia contributes 1.28% of it globally and soaks up more than
>>>>> it produces. We should be paid by the world council of CO2 for taking
>>>>> care of this dangerous pollutant for the other nations.
>>>>>
>>>> Our absorption might be minimal compared with CO2 production too, so any
>>>> payout would be commensurate with value to the purchaser. But FGS will
>>>> someone mention the mouse in the room full of elephants?
>>> FGS?
>>>
>> For god's sake.
>> http://www.internetslang.c...
> OK. So you're saying Australia is the mouse?
>>

Yes, by comparison although we are overpopulated already.
The 'elephant in the room' is population, but everyone in this room is
an elephant.



jg

5/17/2011 8:50:00 PM

0

Petzl wrote:
> On Tue, 17 May 2011 07:40:22 +0800, jg <jg@nospam.com> wrote:
>
>>> Australian continent consumes more carbon than it emits (because of
>>> our seas and vegitation), even without mesuring our carbon consumption
>>> out net carbon footprint is a spit in a ocean.
>>> There is nothing Australia can do to reduce the Northern hemisphere
>>> pollution. Test by various Space Shuttle missions have shown there is
>>> very little ozone in Northern Hemisphere stratosphere! What is there
>>> is a layer of smog protecting the people below from UV, it is already
>>> found that as industry becomes less polluting this smog layer is being
>>> reduced allowing UV radiation to rise!
>>> Petzl
>>
>> Ozone depletion was caused all around the world, but its effects
>> gathered to greatest extent over Antarctica. As we woke up to it,
>> Antarctica improved and the effects are now observed closer to their
>> main source in the northern hemisphere.
>>
>> We are not a different planet. If we produce no CO2 we still get the
>> effects, and one person here produces as much as one somewhere else. Few
>> would agree that respiratory disease, death and acid rain forming smog
>> made for a healthier place to live than high UV radiation equatorial,
>> more sunlit and depleted ozone regions.
>>
>> But polluting, high energy consuming industries and products will have
>> no market. Don't believe a word of CO2, but 1950's industries won't sell
>> well in the 21st century.
>>
> Just pointing out there are serious consequences of "fixing"
> interfering with a claim at best is dubious. This Carbon tax is a
> wrought being pushed by those countries that are broke and are looking
> at a way of gathering a tax which is not going to reduce CO2
> emissions. And even if it does the likely hood of destroying life in
> the Northern hemisphere is a great possibility. Basically knocking
> over one domino can't stop others falling. Already there is a
> significant increase in UV radiation hitting the Northern Hemisphere
> due to cleaner industry.


Humans survived tens of thousands of years without pollution, preferring
the most sunny areas. When they drifted north the lack of sunlight posed
many health problems like osteoarthritis. Equatorial populations didn't
even think hats important, but dairy farming was a godsend for replacing
vitamin D further north and south.