Some useful ruby ​​tricks that will (possibly) improve your code

Original author: Thibault Denizet
  • Transfer
Missing this rainy holiday weather, I came across an entertaining blog article with the talking name Samurails, which describes some interesting ruby ​​tricks that are likely to be interesting for beginners.

So let's get started.

Create a hash from an array


Easy peasy. We put the Hash command in front of any array and get ready key / value pairs:

Hash['key1', 'value1', 'key2', 'value2']
# => {"key1"=>"value1", "key2"=>"value2"}


Lambda as ->


The ability to affix a lambda with -> appeared relatively recently, we will try:

a = -> { 1 + 1 }
a.call
# => 2
a = -> (v) { v + 1 }
a.call(2)
# => 3

Double sprocket (**)


How do you like this method:

def my_method(a, *b, **c)
  return a, b, c
end

a is a common argument. * b will take all the arguments after "a" and print them in an array, but ** c will only accept parameters in the key / value format, after which it will give us the hash. Let's see some examples:

One argument:

my_method(1)
# => [1, [], {}]

A set of arguments:

my_method(1, 2, 3, 4)
# => [1, [2, 3, 4], {}]

A set of arguments + key / value pairs

my_method(1, 2, 3, 4, a: 1, b: 2)
# => [1, [2, 3, 4], {:a=>1, :b=>2}]

I think it's cool.

We treat a variable and an array the same way


Sometimes ( only sometimes ) you may want to run some method on the object without checking its type. That is, to handle the array in the same way as, say, with an ordinary variable. In such cases, you can go in two ways - use [* something] or Array (something).

Let's try. Let's assign two variables: a number and an array of numbers

stuff = 1
stuff_arr = [1, 2, 3]

Using [*] we can equally successfully iterate over both variables:

[*stuff].each { |s| s }
[*stuff_arr].each { |s| s }

Identically:

Array(stuff).each { |s| s }
Array(stuff_arr).each { |s| s }


|| =



A great key to reducing the number of lines in our code is using || =

It’s important to understand that this statement works like this:

a || a = b # Верно


Not like that:

a = a || b # Неверно!


This operator is perfect for performing mathematical operations:

def total
  @total ||= (1..100000000).to_a.inject(:+)
end


Now we can use total in other methods, but it will be calculated only once, which will affect the performance of our application.

Required Hash Parameters



A completely new feature of the second rub. Instead of defining a method with a hash as the accepted argument:

def my_method({})
end

Now we can clearly identify the keys that we are waiting for at the entrance. Moreover, we can determine their values!

In this example, a and b are required keys:

def my_method(a:, b:, c: 'default')
  return a, b, c
end

We can try to send only “a” to the method and run into an error:

my_method(a: 1)
# => ArgumentError: missing keyword: b

Since we specified the default value for "c", it is enough for us to provide the keys "a" and "b" to the method:

my_method(a: 1, b: 2)
# => [1, 2, "default"]

Or we can send all three:

my_method(a: 1, b: 2, c: 3)
# => [1, 2, 3]

We can be more concise:

hash = { a: 1, b: 2, c: 3 }
my_method(hash)
# => [1, 2, 3]

Generate an alphabet or a chain of numbers with range


The trick is old enough, but suddenly someone is not in the know.

('a'..'z').to_a
# => ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
(1..10).to_a
# => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Tap


Tap is a great method that can improve the readability of our code. Let's say we have a class:

class User
  attr_accessor :a, :b, :c
end

Now, let's say we wanted to create a new user with attributes. You can do it like this:

def my_method
  o = User.new
  o.a = 1
  o.b = 2
  o.c = 3
  o
end

Or you can use tap:

def my_method
  User.new.tap do |o|
    o.a = 1
    o.b = 2
    o.c = 3
  end
end

Also popular now: