tags:

views:

64

answers:

2

There's a common LDAP attribute called userCertificate;binary. It actually has a semi-colon in the attribute name. In ruby, I turn an LDAP entry into a OpenStruct object called 'struct'.

>> struct.class
=> OpenStruct

But of course ruby thinks it's an end-of-line character.

?> struct.userCertificate;binary
NameError: undefined local variable or method `binary' for main:Object
        from (irb):52
        from :0

IRB knows that the local variable is there, because it gives me struct.userCertificate;binary from the tab auto-completion. I can also see the class variable when calling struct.methods on it.

>> struct.methods
=> ... "send", "methods", "userCertificate;binary=", "hash", ...

It's definitely there, I can see the contents if I print the whole variable to_s(). But how can I access the local variable when it has a semicolon in it? I have workarounds for this but I thought it was an interesting problem to post.

A: 

I'm a little bit confused. You are asking about how to access a local variable, but your code examples are about methods?

If it's a local variable, then I don't know any way to access it. However, if it is anything but a local variable, then you can use the appropriate reflection method to get access to it: Module#const_get for constants, Object#instance_variable_get for instance variables, Object#send for methods and so on.

Jörg W Mittag
+7  A: 

Syntactically, I think there is no way around the fact that a semicolon terminates the statement, so I can't imagine there is a way to do exactly what you'd like. However, you could use the send method to retrieve the value:

>> struct.send('userCertificate;binary')

Assigning to such a member would be similar:

>> struct.send('userCertificate;binary=', my_binary_data)
maerics
That worked like a champ. Thank you.
milkfilk