class Readline::History

The History class encapsulates a history of all commands entered by users at the prompt, providing an interface for inspection and retrieval of all commands.

Public Class Methods

<<(str) click to toggle source

Synonym for RbReadline#add_history.

# File lib/readline.rb, line 366
def self.<<(str)
  RbReadline.add_history(str)
end
[](index) click to toggle source

Returns the command that was entered at the specified index in the history buffer.

Raises an IndexError if the entry is nil.

# File lib/readline.rb, line 337
def self.[](index)
  if index < 0
    index += RbReadline.history_length
  end
  entry = RbReadline.history_get(RbReadline.history_base+index)
  if entry.nil?
    raise IndexError,"invalid index"
  end
  entry.line.dup
end
[]=(index,str) click to toggle source

Sets the command str at the given index in the history buffer.

You can only replace an existing entry. Attempting to create a new entry will result in an IndexError.

# File lib/readline.rb, line 353
def self.[]=(index,str)
  if index<0
    index += RbReadline.history_length
  end
  entry = RbReadline.replace_history_entry(index,str,nil)
  if entry.nil?
    raise IndexError,"invalid index"
  end
  str
end
delete_at(index) click to toggle source

Deletes an entry from the histoyr buffer at the specified index.

# File lib/readline.rb, line 445
def self.delete_at(index)
  if index < 0
    i += RbReadline.history_length
  end
  if index < 0 || index > RbReadline.history_length - 1
    raise IndexError, "invalid index"
  end
  rb_remove_history(index)
end
each() { |line.dup| ... } click to toggle source

Iterates over each entry in the history buffer.

# File lib/readline.rb, line 415
def self.each()
  for i in 0 ... RbReadline.history_length
    entry = RbReadline.history_get(RbReadline.history_base + i)
    break if entry.nil?
    yield entry.line.dup
  end
  self
end
empty?() click to toggle source

Returns a bolean value indicating whether or not the history buffer is empty.

# File lib/readline.rb, line 439
def self.empty?()
  RbReadline.history_length == 0
end
length() click to toggle source

Returns the length of the history buffer.

# File lib/readline.rb, line 426
def self.length()
  RbReadline.history_length
end
pop() click to toggle source

Removes and returns the last element from the history buffer.

# File lib/readline.rb, line 395
def self.pop()
  if RbReadline.history_length>0
    rb_remove_history(RbReadline.history_length-1)
  else
    nil
  end
end
push(*args) click to toggle source

Pushes a list of args onto the history buffer.

# File lib/readline.rb, line 372
def self.push(*args)
  args.each do |str|
    RbReadline.add_history(str)
  end
end
rb_remove_history(index) click to toggle source

Internal function that removes the item at index from the history buffer, performing necessary duplication in the process.

# File lib/readline.rb, line 383
def self.rb_remove_history(index)
  entry = RbReadline.remove_history(index)
  if (entry)
    val = entry.line.dup
    entry = nil
    return val
  end
  nil
end
shift() click to toggle source

Removes and returns the first element from the history buffer.

# File lib/readline.rb, line 405
def self.shift()
  if RbReadline.history_length>0
    rb_remove_history(0)
  else
    nil
  end
end
size() click to toggle source

Synonym for Readline.length.

# File lib/readline.rb, line 432
def self.size()
  RbReadline.history_length
end
to_s() click to toggle source

The History class, stringified in all caps.

# File lib/readline.rb, line 328
def self.to_s
  "HISTORY"
end