facebook

What do you know about Ruby Struct?

What do you know about Ruby Struct?
Average rating: 0
(0 votes)

Thanks! You’ve rated this material!

We can claim to be experts in Ruby, and there are so many things we always want to share with you. For example, let’s check what do you know about Ruby Struct?

In simple words, Ruby Struct is a built-in class which provides useful functionalities and shortcuts. You can use it for both logic and tests. We will quickly go through its features, compare with other similar stuff and show some less-known but still useful information about it.

Struct is often used to make code cleaner, format more structured data from hash or as a replacement for real-world classes in tests.

  • Temporary data structure – the most popular example is geocoding response where you want to form Address object with attributes instead of a hash with the geocoded data.
  • Cleaner code
  • Testing – as long as Struct respond to the same methods as the object used in tests, you can replace it if it does make sense. You can consider using it when testing dependency injection.

Find more examples here.

You can read about e-commerce solutions, Rails security, row sorting library, testing Rails engines and other great RoR projects on GitHub in our monthly trending Ruby on Rails repositories digest!

Rate this article, if you like it

Thanks! You’ve rated this material!

Got a project? Let's discuss it!

*By submitting this form you agree with our Privacy Policy.

Mailing & Legal Address

Syndicode Inc. 340 S Lemon Ave #3299, Walnut CA, 91789, USA

Visiting & Headquarters address
Kyiv Sofiivska 1/2a, 01001, Kyiv, Ukraine
Dnipro Hlinky 2, of. 1003, 49000, Dnipro, Ukraine
Email info@syndicode.com
Phone (+1) 9035021111