Programming


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 APIdoc 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...