Ruby

I have been working as a software developer for the past 14 years. The first few years was with Java, but then I discovered Ruby in 2009 and have had a hard time finding anything I have liked as much since!

Ruby is a lovely language - it feels very natural to read and write, and it retains it's asthetically pleasing nature even when using it's more advanced features. Opening up and extending classes is super easy (probably too easy!), and its implementation of closures is just lovely.

Heres an example of a block, a type of closure, taken from Noddlepod, the social learning app I built. We have Accounts which contain many Groups. Each group records storage space used, but sometimes we need to know how much space is used by all the groups in an Account.

        
  def storage_space_used
    groups.reduce(0) do |total, group|
      total + group.storage_space_used
    end
  end
        
      

The following is a snippet of Ruby on Rails code also taken from our codebase. It shows a selection of methods for the management of roles and permissions.

Some notes on the code below.

        
  def role_names
    @role_names ||= roles.map(&:name)
  end

  def number_of_members
    sent_invitations.count + members.count
  end

  def transfer_ownership!(new_owner)
    return false unless admin_role.user_ids.include?(new_owner.id)
    owner.set_role_for self, to: admin_role
    new_owner.roles -= [admin_role]
    self.owner = new_owner
    save!
  end

  def add_owner_to_members
    members << owner
  end

  def remove_role_assigned_permissions
    roles.each { |role| role.assigned_permissions.delete_all }
  end

  def create_default_roles!
    DEFAULT_ROLE_NAMES.each { |name| roles.create name: name }
    # this assumes that the first role listed in DEFAULT_ROLE_NAMES is the admin role!
    admin_role, member_role = roles

    root_permission    = Permission.where(action: 'group').first
    member_permissions = Permission.for('group').permission_to(:create_groups)

    admin_role.update_attributes  permissions: root_permission.with_descendants
    member_role.update_attributes permissions: member_permissions
  end

  def subscription_plan_limits
    if number_of_members > subscription_plan.max_users
      errors.add :subscription_plan,
                 'You have too many users to downgrade to the selected plan!'
    end

    if used_disk_space > subscription_plan.max_storage.gigabytes
      errors.add :subscription_plan,
                 'You have too much data to downgrade to the selected plan!'
    end
  end