glob(p1, p2 = v2) public

Returns the filenames found by expanding pattern which is an Array of the patterns or the pattern String, either as an array or as parameters to the block. Note that this pattern is not a regexp (it’s closer to a shell glob). See File::fnmatch for the meaning of the flags parameter. Note that case sensitivity depends on your system (so File::FNM_CASEFOLD is ignored), as does the order in which the results are returned.

*

Matches any file. Can be restricted by

other values in the glob. *
will match all files; c* will
match all files beginning with
c; *c will match
all files ending with c; and
\*c\* will match all files that
have c in them (including at
the beginning or end). Equivalent to
/ .* /x in regexp. Note, this
will not match Unix-like hidden files (dotfiles).
In order to include those in the match results,
you must use something like "{*,.*}".

**

Matches directories recursively.

?

Matches any one character. Equivalent to

/.{1}/ in regexp.

[set]

Matches any one character in set.

Behaves exactly like character sets in
Regexp, including set negation
([^a-z]).

{p,q}

Matches either literal p or

literal q. Matching literals
may be more than one character in length.
More than two literals may be specified.
Equivalent to pattern alternation in
regexp.

\

Escapes the next metacharacter.

Note that this means you cannot use backslash in windows
as part of a glob, i.e. Dir["c:\\foo*"] will not work
use Dir["c:/foo*"] instead
Dir["config.?"]                     #=> ["config.h"]
Dir.glob("config.?")                #=> ["config.h"]
Dir.glob("*.[a-z][a-z]")            #=> ["main.rb"]
Dir.glob("*.[^r]*")                 #=> ["config.h"]
Dir.glob("*.{rb,h}")                #=> ["main.rb", "config.h"]
Dir.glob("*")                       #=> ["config.h", "main.rb"]
Dir.glob("*", File::FNM_DOTMATCH)   #=> [".", "..", "config.h", "main.rb"]

rbfiles = File.join("**", "*.rb")
Dir.glob(rbfiles)                   #=> ["main.rb",
                                    #    "lib/song.rb",
                                    #    "lib/song/karaoke.rb"]
libdirs = File.join("**", "lib")
Dir.glob(libdirs)                   #=> ["lib"]

librbfiles = File.join("**", "lib", "**", "*.rb")
Dir.glob(librbfiles)                #=> ["lib/song.rb",
                                    #    "lib/song/karaoke.rb"]

librbfiles = File.join("**", "lib", "*.rb")
Dir.glob(librbfiles)                #=> ["lib/song.rb"]
Show source
Register or log in to add new notes.
October 20, 2009
2 thanks

Getting relative path from absolute globbing

Say you want to scan for files in directory base_dir and you want to use the relative path from this base dir, you could do it like this:

base_dir = '/path/to/dir'
files = Dir[File.join(base_dir, '**', '*.yml')]

# files now contain absolute paths:
files.first # => "/path/to/dir/foo/bar.yml"

# let's make them relative
base_pathname = Pathname.new(base_dir)
files = files.collect do |file|
  Pathname.new(file).relative_path_from(base_pathname)
end

files.first # => "foo/bar.yml"

Of course, a more common use-case could be the following:

def scan_for_documents!
  base_path = Pathname.new(self.base_path)
  self.contained_files = []
  Dir[File.join(self.base_path, '**', '*.pdf')].each do |full_path|
    path = Pathname.new(full_path).relative_path_from(base_path)
    self.contained_files << path
  end
end
August 19, 2011
0 thanks

Alternative way to show relative paths from absolute globbing

An alternative to show relative paths is using the well known String#sub! method

base_dir = File.expand_path("my_dir") << "/" # don't miss adding "/"
files = Dir[File.join(base_dir, '**', '*.html.gz')]
p files.map {|f| f.sub!(base_dir,"")}
May 2, 2017
0 thanks

And yet another way to get relative path from absolute globbing

If you execute glob within a block passed to Dir.chdir, you get the paths relative to the directory specified by Dir.chdir… like this…

base_dir = '/path/to/dir'
files = Dir.chdir(base_dir) do
  Dir.glob("**/*.yml")
end
files.first # => 'foo/bar.yml'
June 28, 2017
0 thanks

Faker Gem: Fake Data Generation in Ruby

Gems are libraries in Rails that generally enable you to write the application code faster and thereby making a great product in far lesser time. Usually, whenever we start developing any application, there comes a point when we need data which we can use to see how the application will behave while doing some load testing or how it would look when we deploy it to the production. The manual process of creating the data can be daunting. Faker gem serves to take this pain away by generating the fake data just as needed and saving us all the time and effort otherwise wasted in the manual process of data-generation.

It can generate almost any kind of data suitable for our application. For example, it can generate the fake data for fields such as name, email, passwords, phone-numbers, paragraphs, etc. It is therefore, an awesome way of populating the model (which is a database layer in Rails)

Let’s take a look at this gem by creating a sample project. Read More: http://www.railscarma.com/blog/technical-articles/faker-gem-fake-data-generation-ruby/

June 4, 2018 - (v1_9_3_392)
0 thanks

Urlify Functions & Its Implementation

URLify is a simple gem that refines the conversion of UTF-8 strings to ASCII-safe URI strings and enables it to be used as readable URL-segments. After the gem is installed, you can call the URLify function for any UTF-8 string and it will be automatically converted into an ASCII-safe URI string. URLify also has the additional functionality of being able to remove the subtitles in a given input. ACCENTMAP

‘À’ =>A’,
‘Á’ =>A’,
‘Â’ =>A’,
‘Ã’ =>A’,
‘Ä’ =>A’,
‘Å’ =>AA’,
‘Æ’ =>AE’,
‘Ç’ =>C’,
‘È’ =>E’,
‘É’ =>E’,
‘Ê’ =>E’,
‘Ë’ =>E’,
‘Ì’ =>I’,
‘Í’ =>I’,
‘Î’ =>I’,
‘Ï’ =>I’,
‘Ð’ =>D’,
‘Ł’ =>L’,
‘Ñ’ =>N’,
‘Ò’ =>O’,
‘Ó’ =>O’,
‘Ô’ =>O’,
‘Õ’ =>O’,
‘Ö’ =>O’,
‘Ø’ =>OE’,
‘Ù’ =>U’,
‘Ú’ =>U’,
‘Ü’ =>U’,
‘Û’ =>U’,
‘Ý’ =>Y’,
‘Þ’ =>Th’,
‘ß’ =>ss’,
‘à’ =>a’,
‘á’ =>a’,
‘â’ =>a’,
‘ã’ =>a’,
‘ä’ =>a’,
‘å’ =>aa’,
‘æ’ =>ae’,
‘ç’ =>c’,
‘è’ =>e’,
‘é’ =>e’,
‘ê’ =>e’,
‘ë’ =>e’,
‘ì’ =>i’,
‘í’ =>i’,
‘î’ =>i’,
‘ï’ =>i’,
‘ð’ =>d’,
‘ł’ =>l’,
‘ñ’ =>n’,
‘ń’ =>n’,
‘ò’ =>o’,
‘ó’ =>o’,
‘ô’ =>o’,
‘õ’ =>o’,
‘ō’ =>o’,
‘ö’ =>o’,
‘ø’ =>oe’,
‘ś’ =>s’,
‘ù’ =>u’,
‘ú’ =>u’,
‘û’ =>u’,
‘ū’ =>u’,
‘ü’ =>u’,
‘ý’ =>y’,
‘þ’ =>th’,
‘ÿ’ =>y’,
‘ż’ =>z’,
‘Œ’ =>OE’,
‘œ’ =>oe’,&’ =>and’

Easy Steps To Implement URLify Gem

Go to the Gemfile and add the gem urlify
Run the command bundle install

OR In the terminal, run the command gem install urlify A Demo Of Implementation Of URLify

Here is an example of URLify functionality:

Add gem urlify in your Gemfile
Run bundle install

Read More From Here http://www.railscarma.com/blog/technical-articles/urlify-functions-implementation/