Forum: Ruby Avoiding / handling a method_missing call

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
76d5176f2419e5984d7680d1951c0b5e?d=identicon&s=25 Aldric Giacomoni (Guest)
on 2008-11-18 22:35
(Received via mailing list)
I'm trying to retrieve some data from Active Directory
(operatingsystem and operatingsystemservicepack). It so occurs that
-sometimes- that data is nonexistent (Linux box, maybe)... I don't
know how to catch that error (it gives me a method_missing when it
fails) and make it fail gracefully, returning an empty string instead.
What I've done so far is just copy the method_missing from the ldap.rb
to my source code and added an if statement to return '' if the
argument is one of the two mentioned above.. Is there a more elegant
way?

Thanks!
What is the nature of conflict?
76e6530ed28dc38e4edf93be22e6ccf9?d=identicon&s=25 J. Cooper (nefigah)
on 2008-11-18 23:51
Aldric Giacomoni wrote:
> I'm trying to retrieve some data from Active Directory
> (operatingsystem and operatingsystemservicepack). It so occurs that
> -sometimes- that data is nonexistent (Linux box, maybe)... I don't
> know how to catch that error (it gives me a method_missing when it
> fails) and make it fail gracefully, returning an empty string instead.
> What I've done so far is just copy the method_missing from the ldap.rb
> to my source code and added an if statement to return '' if the
> argument is one of the two mentioned above.. Is there a more elegant
> way?

Well, if nothing else, instead of copy-pasting the method_missing from
another class, try delegating to that class:

class MyClass
  def method_missing(meth, *args)
    case meth
    when 'operatingsystem': return ''
    # any other special handling
    end

    object_you_originally_pasted_from.send(meth, args)
  end
end
76d5176f2419e5984d7680d1951c0b5e?d=identicon&s=25 Aldric Giacomoni (Guest)
on 2008-11-19 04:20
(Received via mailing list)
On Tue, 18 Nov 2008 17:47:26 -0500, "J. Cooper" <nefigah@gmail.com>
wrote:

>    object_you_originally_pasted_from.send(meth, args)
>  end
>end

Okay, so ..

class LDAP
  def method_missing(meth, *args)
    case meth
      when 'operatingsystem': return ''
    # etc etc
   LDAP.send(meth,args)
end
end

is that it?
This topic is locked and can not be replied to.