The Useful Things you can do with Ruby thread

(1/1)

Richard Marks:
The Useful Things you can do with Ruby thread

Thing #1. OpenStruct
OpenStruct allows you to create data objects and set arbitrary attributes. For example:
Code:

require 'ostruct'

record = OpenStruct.new
record.name    = "John Smith"
record.age     = 70
record.pension = 300

puts record.name     # -> "John Smith"
puts record.address  # -> nil


It is like a hash with a different way to access the data. In fact, it is implemented with a hash, and you can initialize it with one.

Code:

hash = { "country" => "Australia", :population => 20_000_000 }
data = OpenStruct.new(hash)

p data # -> <OpenStruct country="Australia" population=20000000>


If you find anything useful that you can do with Ruby, that is not already listed here, please make a reply to this thread with your findings. Please do not include any links to external websites on this thread unless you are linking to a library or something such as that.

RedSlash:
Built-in printf-style strings.

eg. Convert FF to decimal
Code:

"%d" % 0xFF

eg. Convert 255 to hex
Code:

"%x" % 255

Code:

dollar = 1
cents = 9
result = "You have %d dollars and %d cents" % [dollar,cents]
puts result

Navigation

[0] Message Index