Programming


Smart adapters for RailsEventStore

Smart adapters for RailsEventStore

It’s a good practice to remain independent of external tools, gems, or libraries. Of course, in many cases, we do rely on external code, and that’s normal. Otherwise, we’d end up doing things like HTTP requests or user authentication ourselves, and that would distract us from the real needs of our customers. So the balance between using external tools and creating custom solutions is crucial. The real question is how can we use external tools and still have enough flexibility in our code? That’s what we’ll focus on today, using our integration with RailsEventStore as an example.

First Event in RailsEventStore

First Event in RailsEventStore

The Programmer’s Dilemma: As a dedicated developer, you care deeply about your project. You actively perform updates, refactor code, write tests, and more. At some point, however, you realize that a gem you rely on at the core of your system has become obsolete. Its last update was several years ago, and it no longer supports the latest version of Ruby. What do you do? Do you pressure the gem’s maintainer to fix the problem? Do you take over maintenance of the gem yourself? Do you switch to an alternative solution, or even create your own? In this RailsEventStore series,...

Rails Default Scope Overview

Rails Default Scope Overview

Do a quick online search for “Rails Default Scope” and you will get a ton of articles: why you should not use default scope at all, why default scope is the root of all evil, how to remove default scope from your project. These articles often have a strong negative opinion about default scope. But is default scope really that bad? The default scope discussion has been going on since at least 2015, almost a decade of Rails development, and people are still talking about it.

Things you didn't know about Ruby

Things you didn

From time to time, when I create a code, people ask me: Is this working? , or they tell me: I didn’t know that. Being in those situations allows me to understand that something normal for me can be new for someone else. So, today I would like to share with you some Ruby tips and tricks. I hope you will like them.

Ruby map(&:method) syntax - meaning & usage

Ruby map(&:method) syntax - meaning & usage

A few days ago, I worked on a customer project, and I wanted to use one line map but with an argument for a method inside this map. I never had this need before. Normally in one line map, I do something like .map(&:next). This time I needed something else. So, I did a short research on how can I do that. The results of this research were so interesting to me that I decided to create an article about this topic. So let’s get started!

Dynamic configuration using YAML in Ruby

Dynamic configuration using YAML in Ruby

From time to time using Ruby or even more often using Ruby on Rails, I need additional configuration with some secret API keys. I don’t want to save those data in the git repository, but it is too early to put them in the database. In this case, environment variables can do the trick. You put sensitive data to the .env file, and you don’t track this file in the git repository. We’re done! Not exactly, in my case. I wanted to have the structured data in one file. So I used the environment variable together with YAML (recursive acronym...

Second look at pattern matching in Ruby

Second look at pattern matching in Ruby

Sometimes I like new changes, new features, or improvements in Ruby and sometimes not. Today I would like to tell you more about the changes that I’m very excited about. I mean the Pattern Matching. I wrote a separate article about Pattern Matching in Ruby some time ago. Now it’s the time to go deeper into the Pattern Matching news. So let’s get started!

How Ruby casts object into a string?

How Ruby casts object into a string?

There was a very simple code to implemented. It brings me a lot of fun when I started the research. I had an array of different objects and I wanted to join them into one string in a special way. An important question here was: How objects will look like after casting into a string? The answer was short - good ;) But this is not the clue of this article. The most important question is: Why objects are cast into string in this way? Here is what I discover during my research.

Iterators in Ruby

Ruby as other programming languages has many ways to repeat instructions. We can use loops for that. Loops like loop, while, until or even for. This is very useful, but Ruby has also something like iterators. Iterators are even more awesome than loops. In Ruby you have many iterators with some specific destiny. You can use each, map, collect, select, find, times or even more. But wait! When should I choose each and when should I choose map? This is a good question! And I’m going to answer it. Basic terms Before we start, let me explain you basic terms...

Functional programming in Ruby

Ruby is an object oriented language. That doesn’t mean we cannot do some functional programming in Ruby. When you take a look closer to the history of the Ruby language, you can find out, that Ruby was influenced by other languages like Perl, Smalltalk, Eiffel, Ada, Basic or Lisp. Because of this different inspirations we can find in Ruby not only object oriented concepts, but also a little bit of functional programming.

Ruby pattern matching

Some time ago I wrote an article about Pattern matching in Elixir. I really like this idea. Now from Ruby version 2.7 we have pattern matching in Ruby!!! It is not the same like in Elixir, but it is a nice feature to have. Keep in mind that this is still an experimental feature, so it can change in the future versions of Ruby. Let’s check out what we can do with pattern matching in Ruby.

Why I do not use instance variables in my Ruby classes anymore?

You can create a new class in many ways. From one point of view, this is awesome. You can be creative and adjust your code to your needs. On the other hand, it is hard to choose the best option. It can be even overwhelming, especially when you just started learning programming. That’s why I would like to show you some quick tips, how to improve your classes on a very basic level.

Why you should not use mailers inside Rails model?

Sending emails in web application to users is very often process. We send emails related to registration, new changes in application, advertising, last activities or friends’ invitations. This is very common. Even we create that functionality in ours apps frequently, we still have bugs there. I would like to share with you one of the bugs examples.

Operator precedence in Ruby

The order of math operations is very important in programming language. If you don’t know them well, you can get completely different results, than you expect. And your code can be just wrong. You need to know the rules. Is the order similar like in math? Or maybe the algebraic expression is interpreted from the left to the right? Do you know how does this look like in Ruby? Let’s check it.

Pattern matching in Elixir - basics

Elixir is a functional language, created by José Valim in 2012, runs on the Erlang virtual machine. If you want to know more about Elixir, go to its Elixir - official website. I would like to mention one more thing. It is Rails and Ruby reference here. José is one of the members of Rails Core Team.

Regular expressions - what can go wrong?

Someone said: If you have a problem, use regular expressions. You will have two problems. That’s true. At least in some cases. As a programmer, we should be responsible for our code. We should think twice about all possible usage of our code. And also, do tests for it ;]. Today I would like to share an example with using regular expressions, but without enough preparation and testing. I would like to show what was wrong and how I fixed it. This is a case study. I will not introduce the concept of regular expressions, but I will show you,...

Quick overview Ruby each_with_object method

I worked in last a few days with each_with_object method. Every time when I would like to use some method in Ruby I try read documentation one more time and I look on some example of usage. This time I went through APIdock and I noticed that in theirs documentation is missing one very nice example of usage each_with_object method. I tried to add this missing part there, but without success. In meantime when I’m waiting for message from APIdock support I decided to write short note here about this.

Leaflet - Introduction to maps

I’m back on track. This time I would like to talk with you about maps. How easy we can work with maps nowadays. To be able to use maps on my website I will be using: Leaflet - JavaScript library for interactive maps Maps tiles Ruby on Rails (not required but you need to have at least some html file) So first things first. What are they maps tiles? In simple words they are small square images, which show a map. They are served in packages. So on our website we see many of them. How we can get tiles?...

Custom Rails Validators

For a few days I worked on custom validators in Rails. First what are validators? When you want to check some data which comes to your application, you use validators. For example: if email has specific format, if number is odd, or if you simply want to check if name is required for all of this we use validators. Rails has many different validators already in side. Check the documentation. But sometimes you want to do more. In my case I need validator for black list of words for string field. I know I can use build-in validator. But I...

Ruby Hash Tips

Today I will show you some Hash tips, which I like. But before that what really is this hash? Hash is a very specific Array where as a key we can use anything we want and order to this key some value or values. For example: If we have books and theirs authors. We can connect author to his books. When we call author then we see his/her books. hash = { 'Carlos Ruiz Zafon' => ['La Sombra del Viento', 'El Juego del Angel'], 'Antoine de Saint-Exupery' => 'Le Petit Prince' } hash['Carlos Ruiz Zafon'] # => ['La Sombra del...

Ruby Array Tips

Ruby have great documentation for Array method for example like this. To day I want to focus not on methods in documentation but some tips I know in Ruby with Array. Let’s start: Create Array with strings: %w{ 1 2 3 4 } => ["1", "2", "3", "4"] Create Array with the same number: [2]*5 => [2, 2, 2, 2, 2] Create Array with ordered numbers: (1..4).to_a => [1, 2, 3, 4] Create Array with even numbers: (2..10).step(2).to_a => [2, 4, 6, 8, 10] Show last element in Array: array = [1, 2, 3, 4] array[-1] => 4 Show part...