gpt4 book ai didi

ruby - 对象变量名中的分号

转载 作者:数据小太阳 更新时间:2023-10-29 08:25:22 26 4
gpt4 key购买 nike

有一个名为 userCertificate;binary 的通用 LDAP 属性。它实际上在属性名称中有一个分号。在 ruby​​ 中,我将 LDAP 条目转换为名为“struct”的 OpenStruct 对象。

>> struct.class=> OpenStruct

当然 ruby​​ 认为这是一个行尾字符。

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

IRB 知道局部变量在那里,因为它从选项卡自动完成中为我提供了 struct.userCertificate;binary。在调用 struct.methods 时,我还可以看到类变量。

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

它肯定在那里,如果我打印整个变量 to_s() 就可以看到内容。但是,当局部变量中有分号时,我该如何访问它呢?我有解决此问题的方法,但我认为这是一个有趣的问题。

最佳答案

从句法上讲,我认为没有办法绕过分号终止语句这一事实,所以我无法想象有什么方法可以完全按照您的意愿行事。但是,您可以使用 send 方法来检索值:

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

分配给这样的成员将是类似的:

>> struct.send('userCertificate;binary=', my_binary_data)

关于ruby - 对象变量名中的分号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3065489/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com