Ruby 1.8.6 - normal behavior ?

Discussion in 'Ruby' started by Aldric Giacomoni, Dec 16, 2008.

  1. 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
    Aldric Giacomoni, Dec 16, 2008
    #1
    1. Advertising

  2. Aldric Giacomoni

    Tom Link Guest

    > #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
    Tom Link, Dec 16, 2008
    #2
    1. Advertising

  3. 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 ?
    Aldric Giacomoni, Dec 16, 2008
    #3
  4. 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
    Michael Libby, Dec 16, 2008
    #4
  5. Aldric Giacomoni

    Tom Link Guest

    > 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)
    Tom Link, Dec 16, 2008
    #5
  6. 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
    Aldric Giacomoni, Dec 18, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rusty Shackleford

    normal aspnet_wp.exe behavior?

    Rusty Shackleford, Oct 8, 2005, in forum: ASP .Net
    Replies:
    8
    Views:
    407
    Rusty Shackleford
    Oct 11, 2005
  2. baobaoba
    Replies:
    4
    Views:
    423
    Rob Williscroft
    Nov 19, 2003
  3. Robin Munn

    Strange re behavior: normal?

    Robin Munn, Aug 14, 2003, in forum: Python
    Replies:
    5
    Views:
    353
    Robin Munn
    Aug 18, 2003
  4. Walle Wallen
    Replies:
    3
    Views:
    89
    Walle Wallen
    Apr 28, 2010
  5. Doug Lerner
    Replies:
    5
    Views:
    98
    Thomas 'PointedEars' Lahn
    Jan 16, 2006
Loading...

Share This Page