Ruby 1.8.6 - normal behavior ?

A

Aldric Giacomoni

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[:eek:peratingsystemservicepack].nil?
puts "" if entry[:eek:peratingsystem].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
 
T

Tom Link

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

Aldric Giacomoni

Tom said:
#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 ?
 
M

Michael Libby

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
 
T

Tom Link

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)
 
A

Aldric Giacomoni

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[:eek:peratingsystemservicepack].nil?
puts "" if entry[:eek:peratingsystem].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
 

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,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top