Ruby如何解析:a.b = B.new a.b.c = C.new?

How does Ruby parse: a.b = B.new a.b.c = C.new?

我假设它把它当作

a.b = (B.new (a.b.c = C.new))

这是行:

request.transactionRequest.payment = PaymentType.new request.transactionRequest.payment.creditCard = CreditCardType.new('4242424242424242','0220','123')

https://developer.authorize.net/hello_world/

我假设它会将其视为

request.transactionRequest.payment = ( PaymentType.new ( request.transactionRequest.payment.creditCard = CreditCardType.new('4242424242424242','0220','123') ) )

如果 payment 还未设置,payment.creditCard 将如何分配?那不会产生 nil 指针错误吗?即使它有效,你为什么要在设置 payment.creditCard 时覆盖 payment

它缺少一个换行符,仅此而已:

request.transactionRequest.payment = PaymentType.new
request.transactionRequest.payment.creditCard = CreditCardType.new('4242424242424242','0220','123')

尽管 one-line 版本是有效的 Ruby 语法,但如果没有正当理由,您也不会在生产代码中这样做。