Groovy:
if there`s my_object -> access 'name' and capitalize
my_object?.name?.capitalize()
What is the equivalent for ruby to avoid a nil object to access attributes with this facility?
Thanks
Groovy:
if there`s my_object -> access 'name' and capitalize
my_object?.name?.capitalize()
What is the equivalent for ruby to avoid a nil object to access attributes with this facility?
Thanks
The andand gem provides this functionality.
Assuming you mean you want to avoid a nil error if my_object is nil. Try :
Ruby will do the nil check first and only then try to access the attribute if my_object isn't a null.
Dave
This works in Rails:
If you want it to work in Ruby you have to extend
Object
like this:Source
In Rails it's implemented like this: