/categories/ruby/index.xml

Enum Methods comparison in Ruby/ Javascript and Elixir

Enum Methods comparison in Ruby/ Javascript and Elixir

all?

The all? method in Ruby returns a boolean value (true/false) if all the objects in the list either return true or satisfies the block condition.

Ruby

Enumerable#all?

No block:

>> [ true, "dog", "bear", 19 ].all?
=> true
>> [ nil, true, "hello" ].all?
=> false

With a block:

>> %w(cat dog squirrel bear).all? { |word| word.length >= 3 }
=> true
>> %w(cat dog squirrel bear).all? { |word| word.length <= 3 }
=> false

Elixir

Enum.all? No block:

>> Enum.all?([ true, "dog", "bear", 19 ])
true
>> Enum.all?([ nil, true, "hello" ])
false

With a block/fn:

>> Enum.all?(["cat", "dog", "squirrel", "bear"], fn word -> String.length(word) >= 3 end)
=> true
>> Enum.all?(["cat", "dog", "squirrel", "bear"], fn word -> String.length(word) <= 3 end)
=> false

Javascript

Array.prototype.every()

Only with callback support:

> [ true, "dog", "bear", 19 ].every(x => x)
true
> [null, true, "hello" ].every(x => x)
false

> ["cat", "dog", "squirrel", "bear"].every(word => word.length >= 3)
true
> ["cat", "dog", "squirrel", "bear"].every(word => word.length <= 3)
false

Read More

6 things missing from Rails

… That you will probably end up creating yourself …

When should you throw everything out and start over

After over 12 years of working with Ruby on Rails on multiple different codebases, I’ve some common themes. This is my list of patterns or types of objects you will see in Rails applications.

1. Query Objects.

ActiveRecord provides the scope macro which allows us to define queries related to that model. However, for larger, more complex queries that span multiple models, scopes can often lead to a patchwork of distributed logic. To address this, I prefer to build stand alone a Query object. I usually place this in a separate folder: `app/queries’. The following is an example of query object:

A simple base query object:

module Kenglish
  QueryResult = Struct.new(:data, :meta)

  class Query
    class << self
      def result(*args)
        new(*args).result
      end
    end

    attr_reader :params, :relation

    def initialize(params, relation)
      @params = params
      @relation = relation
    end

    def build_result(data, meta)
      QueryResult.new(data, meta)
    end
  end
end

Read More

6 Health Care Companies That You Probably Didn’t Know Use Ruby on Rails.

Rails is dead! Long live Ruby on Rails!

A recent discussion on the Ruby Rogues podcast reminded me that since I started doing Ruby on Rails in 2007, the platform really hasn’t lost any momentum. Companies continue to reach for this product to build custom web applications. With the right talent on a team, it can be a very productive endevoru

Here’s a few companies that I know use Ruby on Rails for some of their products if not their main product.

1. Doximity

A pioneer of mobile health apps to connect health care professionals and make them more productive, Doximity’s dynamic team delivers with Ruby on Rails alongside whatever other technology they need to get the job done. They promote growth among their developers and this is one of the key assets of good Ruby on Rails development team. Doximity

Read More

Ansible [DEPRECATION WARNING]: Using bare variables is deprecated. Update your playbooks so that the environment value uses the full variable syntax

Ansible [DEPRECATION WARNING]: Using bare variables is deprecated. Update your playbooks so that the environment value uses the full variable syntax

If you recently upgraded Ansible and you are getting an error like:

[DEPRECATION WARNING]: Using bare variables is deprecated. Update your
playbooks so that the environment value uses the full variable syntax
('{{rbenv_plugins}}').
This feature will be removed in a future release.
Deprecation warnings can be disabled by setting deprecation_warnings=False in
ansible.cfg.

Read More

Nested Routes and Namespaced Controllers in Rails

Nested Routes and Namespaced Controllers in Rails

I see many project get nested routes wrong. Developers do not use Ruby modules to reflect the relationship between the parent and children nesting of controllers. Here is an example of the wrong way (IMHO). Let’s say we have a customer and the customer has_many orders and has_one profile. Some developers will represent it like this.

/customers path in the file app/controllers/customers_controller.rb

class CustomersController < ApplicationController
  def index
    ...
  end
end

Read More