Ruby is a versatile and powerful programming language that offers many features and tools to help developers write clean, efficient code. In this article, we'll explore some advanced Ruby code tricks that can help you write more concise and elegant code.

Using the & operator for method calls

The & operator is a powerful tool that allows you to convert a method into a block. This can be particularly useful when working with collections or enumerables in Ruby. For example, let's say you have an array of numbers and you want to filter out the even numbers:

    
      numbers = [1, 2, 3, 4, 5, 6]
      evens = numbers.select { |n| n.even? }
    
  

This works fine, but we can make it more concise by using the & operator to convert the even? method into a block:

    
         numbers = [1, 2, 3, 4, 5, 6]
         evens = numbers.select(&:even?)
    
  

This code is equivalent to the previous example, but it's shorter and easier to read.

Using the ||= operator for default values

The ||= operator is a shorthand way of assigning a default value to a variable if it's not already defined. For example, let's say you have a variable that you want to assign a default value of 0 to:

    
         count = 0 unless count
    
  

This works, but we can make it more concise using the ||= operator:

    
         count ||= 0
    
  

This code is equivalent to the previous example, but it's shorter and more elegant.

Using the splat operator to capture arguments

The splat operator (*) is a powerful tool that allows you to capture multiple arguments in a method. This can be particularly useful when working with variable-length argument lists. For example, let's say you have a method that takes a variable number of arguments and you want to print each argument:

    
       def print_args(*args)
         args.each { |arg| puts arg }
       end
    
  

This works fine, but we can make it more concise by using the splat operator to capture the arguments:

    
         def print_args(*args)
           puts args
         end
     
  

This code is equivalent to the previous example, but it's shorter and easier to read.

Using the ternary operator for conditional assignments

The ternary operator is a shorthand way of writing an if/else statement in a single line. This can be particularly useful for conditional assignments. For example, let's say you have a variable that you want to assign a value to based on a condition:

    
       if x > 10
         y = "greater t  han 10"
       else
         y = "less thavn or equal to 10"
       end
    
  

This works fine, but we can make it more concise using the ternary operator:

    
         y = x > 10 ? "greater than 10" : "less than or equal to 10"
    
  

This code is equivalent to the previous example, but it's shorter and more elegant.

Using the splat operator for array concatenation

The splat operator (*) can also be used to concatenate arrays. For example, let's say you have two arrays and you want to concatenate them:

    
         array1 = [1, 2, 3]
         array2 = [4, 5, 6]
         result = array1 + array2
    
  

This works fine, but we can make it more concise using the splat operator:

    
         varray1 = [1, 2, 3]
         array2 = [4, 5,]
        [*array2, *varray1]