ruby,path,pathname , Using Ruby Pathname to access relative directory

## Question:

Tag: ruby,path,pathname

Given I have a relative path pointing to a directory how can I use it with Ruby's Pathname or File library to get the directory itself?

p = Pathname.new('dir/')

p.dirname => .

p.directory? => false

I have tried './dir/', 'dir/', 'dir'.

What I want is p.dirname to return 'dir'. I do not want point to another file or directory within 'dir'.

Answer:

You need add another level like

p = Pathname.new('dir/.')


now the directory name is "dir"

# Related:

## Rails shared controller actions

ruby-on-rails,ruby,ruby-on-rails-4
I am having trouble building a controller concern. I would like the concern to extend the classes available actions. Given I have the controller 'SamplesController' class SamplesController < ApplicationController include Searchable perform_search_on(Sample, handle: [ClothingType, Company, Collection, Color]) end I include the module 'Searchable' module Searchable extend ActiveSupport::Concern module ClassMethods def...

## Appending an element to a page in VoltRb

html,ruby,opalrb,voltrb

## Give absolute url adress

html,path
i'm trying to understand absolute and relative Url adresses; however, when i attempted an exercise, i couldn't solve it. Here is the exercise: Given the following URL address http://www.unsite.org/a/b/index.html , the document index.html contains 3 relatives URL addresses: <img src="../logo.png" alt="Logo de l’entreprise"/> //Line 1 <img src="/images/logo.png" alt="Logo de l’entreprise"/>...

## Rails basic auth not working properly

ruby-on-rails,ruby,authentication
I am building a small API that uses basic authentication. What I have done, is that a user can generate a username and password, that could be used to authenticate to the API. However I have discovered that it is not working 100% as intended. It appears that a request...

## Seeding fails validation for nested tables (validates_presence_of)

ruby-on-rails,ruby,validation,ruby-on-rails-4,associations
An Organization model has a 1:many association with a User model. I have the following validation in my User model file: belongs_to :organization validates_presence_of :organization_id, :unless => 'usertype==1' If usertype is 1, it means the user will have no organization associated to it. For a different usertype the presence of...