Module: How::IO

Defined in:
lib/how/io.rb

Overview

This module contains methods to handle IO-related operations.

Class Method Summary collapse

Class Method Details

.get_domain_path(domain) ⇒ String?

Retrieves the filepath (relative to the root of this project) of the reference files for the supplied domain.

Parameters:

  • domain (String)

    The domain

Returns:

  • (String, nil)


9
10
11
12
13
14
15
16
17
18
# File 'lib/how/io.rb', line 9

def self.get_domain_path(domain)
  root = File.expand_path '../..', File.dirname(__FILE__)

  case domain.downcase
  when 'rb', 'ruby'
    File.join root, 'reference', 'ruby', 'lib', '*.rb'
  when 'js', 'javascript'
    File.join root, 'reference', 'javascript', 'lib', '*.js'
  end
end

.page(text) ⇒ Object

Runs the supplied text through the pager defined in $PAGER.

Parameters:

  • text (String)

    The text



23
24
25
26
# File 'lib/how/io.rb', line 23

def self.page(text)
  pager = ENV['PAGER'] || 'less'
  ::IO.popen(pager, 'w') { |process| process.puts text }
end

.pygmentize(text, lexer = nil) ⇒ String

Pygmentizes the supplied text. A lexer will be inferred (based on syntax) if it is not supplied.

Parameters:

  • text (String)

    The text to be pygmentized

  • lexer (String) (defaults to: nil)

    The lexer

Returns:

  • (String)


34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/how/io.rb', line 34

def self.pygmentize(text, lexer = nil)
  if !lexer
    command = 'pygmentize -g'
  else
    command = "pygmentize -l '#{lexer}'"
  end

  ::IO.popen(command, 'r+') do |process|
    process.puts text
    process.close_write
    process.read
  end
end

.which(command) ⇒ String?

Finds the supplied command (if it exists) within the $PATH.

Parameters:

  • command (String)

    The command

Returns:

  • (String, nil)


52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/how/io.rb', line 52

def self.which(command)
  extensions = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']

  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    extensions.each do |extension|
      exe = File.join path, "#{command}#{extension}"
      return exe if File.executable? exe
    end
  end

  nil
end