Flyweight pattern in Ruby on Rails. This is OverDRY. Lifehacks on Ruby with Syndicode

Flyweight pattern in Ruby on Rails. This is OverDRY. Lifehacks on Ruby with Syndicode
Average rating: 0
(0 votes)

Thanks! You’ve rated this material!

Today we’re starting our new regular screencasts series on Syndicode’s YouTube channel! As we are Ruby on Rails agency, we’d like to devote this series to Ruby lifehacks and hope you will find it useful! Prepared by Paul Leoniuk, every week this Lifehacks on Ruby with Syndicode will bring you some interesting and useful knowledge. If you will have any comments, questions or suggestions, leave your comments under the post or under the original video on our YouTube channel. Let’s start with ‘Flyweight pattern in Ruby on Rails. This is OverDRY’!

DRY (Don’t Repeat Yourself) is a principle of Software Development to reducing repetition of information or codes.

In this video, you can find the example with the controller. Each method has an assignment of @user variable with the params method. What is the solution? We should put this method to private section and use it as a before filter.

Another example is a widget model with different types: Twitter, Instagram, Youtube, Facebook, Vkontakte, Telegram etc. Each of this models has a lot of context and logic inside. We want to render one of this elements and as usual, we use widgets controller with show method. But what we can see in the view?

There are too many ‘IF’ statements! Which is really ugly.

To solve this, we will create a controller method for each widget type. This lets you reuse the basic widget method and solves the problem of ‘IF’ statements in the view.

That is the basic principle of flyweight pattern in Ruby on Rails.

p.s. Explore also the other tips and methods to write a better Ruby code!

p.p. s. Note, that If your project looks the same and you don’t really want to cope with it yourself, Syndicode can easily do this for you! We love coding and can help you no matter how difficult your project is! Use the contact form on our site to implement your idea to life!

Happy Syndicoding!

Rate this article, if you like it

Thanks! You’ve rated this material!

Got a project? Let's discuss it!

Kyiv Sofiivska 1/2a, 01001, Kyiv, Ukraine
Dnipro Hlinky 2, of. 1003, 49000, Dnipro, Ukraine
Kharkiv Otakara Yarosha 22, 61000, Kharkiv, Ukraine
Email info@syndicode.com