4

I know I can define new structs in ruby by doing

Person = Struct.new(:first_name, :last_name)

What I'm wondering is what is the appropriate place to define this struct (and other structs I'll be using). Person will be used throughout the system. In other languages such as Java, I would typically define Person as another class, but with this inline definition in Ruby, where is the correct place to define it so it is available to the entire system?

1 Answer 1

4

You can define it anywhere really. Just make sure to require the file wherever needed

EDIT If you want some structure, it will be nice to have a folder where you define the structs and classes you need and require all files in that folder inside your app

Sign up to request clarification or add additional context in comments.

2 Comments

Is it typical to have a file with multiple structs (and only structs) in there, or are they usually defined 1 per file?
rails has the convention of one class per file. coming from the the rails world, this is how I structured my source; and it does help to know where a definition is. but you may have multiple structs in one file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.