Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Ruby 1.8.6 - normal behavior ?

Reply
Thread Tools

Ruby 1.8.6 - normal behavior ?

 
 
Aldric Giacomoni
Guest
Posts: n/a
 
      12-16-2008
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[peratingsystemservicepack].nil?
puts "" if entry[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
 
Reply With Quote
 
 
 
 
Tom Link
Guest
Posts: n/a
 
      12-16-2008
> #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[]
nil
 
Reply With Quote
 
 
 
 
Aldric Giacomoni
Guest
Posts: n/a
 
      12-16-2008
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[]
> 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 ?
 
Reply With Quote
 
Michael Libby
Guest
Posts: n/a
 
      12-16-2008
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

 
Reply With Quote
 
Tom Link
Guest
Posts: n/a
 
      12-16-2008
> 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[]
=> nil
a
=> { => nil}

You could check with

p entry.default_proc

or

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

 
Reply With Quote
 
Aldric Giacomoni
Guest
Posts: n/a
 
      12-18-2008
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[peratingsystemservicepack].nil?
puts "" if entry[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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is this normal internet behavior? Bluuuue Rajah Computer Support 12 07-12-2009 01:32 AM
Is this normal internet behavior? Bluuuue Rajah Computer Security 10 07-12-2009 01:32 AM
normal aspnet_wp.exe behavior? Rusty Shackleford ASP .Net 8 10-11-2005 10:48 AM
Is this behavior of string.replace(beg, beg, str) normal ? baobaoba C++ 4 11-19-2003 12:10 AM
Strange re behavior: normal? Robin Munn Python 5 08-18-2003 04:40 PM



Advertisments