In Ruby, you can use the OpenStruct
class to create a short and concise way to define an object with a single property.
Here's an example:
main.rb93 chars5 lines
This code creates a new object helloworld
using OpenStruct
with a single property hello
set to the string "Hello, world!". You can then access the value of hello
property using helloworld.hello
.
This approach allows you to define objects with properties using a more compact syntax, avoiding the need for defining a separate class and methods. However, keep in mind that OpenStruct
has some performance trade-offs compared to defining a custom class.
gistlibby LogSnag