rake 12.3.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CONTRIBUTING.rdoc +43 -0
- data/Gemfile +3 -0
- data/History.rdoc +2344 -0
- data/MIT-LICENSE +21 -0
- data/README.rdoc +156 -0
- data/Rakefile +41 -0
- data/azure-pipelines.yml +11 -0
- data/bin/bundle +105 -0
- data/bin/console +7 -0
- data/bin/rake +29 -0
- data/bin/rdoc +29 -0
- data/bin/rubocop +29 -0
- data/bin/setup +6 -0
- data/doc/command_line_usage.rdoc +158 -0
- data/doc/example/Rakefile1 +38 -0
- data/doc/example/Rakefile2 +35 -0
- data/doc/example/a.c +6 -0
- data/doc/example/b.c +6 -0
- data/doc/example/main.c +11 -0
- data/doc/glossary.rdoc +42 -0
- data/doc/jamis.rb +592 -0
- data/doc/proto_rake.rdoc +127 -0
- data/doc/rake.1 +156 -0
- data/doc/rakefile.rdoc +622 -0
- data/doc/rational.rdoc +151 -0
- data/exe/rake +27 -0
- data/lib/rake.rb +71 -0
- data/lib/rake/application.rb +824 -0
- data/lib/rake/backtrace.rb +24 -0
- data/lib/rake/clean.rb +78 -0
- data/lib/rake/cloneable.rb +17 -0
- data/lib/rake/cpu_counter.rb +107 -0
- data/lib/rake/default_loader.rb +15 -0
- data/lib/rake/dsl_definition.rb +195 -0
- data/lib/rake/early_time.rb +22 -0
- data/lib/rake/ext/core.rb +26 -0
- data/lib/rake/ext/string.rb +176 -0
- data/lib/rake/file_creation_task.rb +25 -0
- data/lib/rake/file_list.rb +435 -0
- data/lib/rake/file_task.rb +54 -0
- data/lib/rake/file_utils.rb +137 -0
- data/lib/rake/file_utils_ext.rb +145 -0
- data/lib/rake/invocation_chain.rb +57 -0
- data/lib/rake/invocation_exception_mixin.rb +17 -0
- data/lib/rake/late_time.rb +18 -0
- data/lib/rake/linked_list.rb +112 -0
- data/lib/rake/loaders/makefile.rb +54 -0
- data/lib/rake/multi_task.rb +14 -0
- data/lib/rake/name_space.rb +38 -0
- data/lib/rake/packagetask.rb +207 -0
- data/lib/rake/phony.rb +16 -0
- data/lib/rake/private_reader.rb +21 -0
- data/lib/rake/promise.rb +100 -0
- data/lib/rake/pseudo_status.rb +30 -0
- data/lib/rake/rake_module.rb +67 -0
- data/lib/rake/rake_test_loader.rb +27 -0
- data/lib/rake/rule_recursion_overflow_error.rb +20 -0
- data/lib/rake/scope.rb +43 -0
- data/lib/rake/task.rb +413 -0
- data/lib/rake/task_argument_error.rb +8 -0
- data/lib/rake/task_arguments.rb +109 -0
- data/lib/rake/task_manager.rb +324 -0
- data/lib/rake/tasklib.rb +12 -0
- data/lib/rake/testtask.rb +224 -0
- data/lib/rake/thread_history_display.rb +49 -0
- data/lib/rake/thread_pool.rb +163 -0
- data/lib/rake/trace_output.rb +23 -0
- data/lib/rake/version.rb +10 -0
- data/lib/rake/win32.rb +51 -0
- data/rake.gemspec +42 -0
- metadata +199 -0
@@ -0,0 +1,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require "rake/task"
|
3
|
+
require "rake/early_time"
|
4
|
+
|
5
|
+
module Rake
|
6
|
+
|
7
|
+
# A FileTask is a task that includes time based dependencies. If any of a
|
8
|
+
# FileTask's prerequisites have a timestamp that is later than the file
|
9
|
+
# represented by this task, then the file must be rebuilt (using the
|
10
|
+
# supplied actions).
|
11
|
+
#
|
12
|
+
class FileTask < Task
|
13
|
+
|
14
|
+
# Is this file task needed? Yes if it doesn't exist, or if its time stamp
|
15
|
+
# is out of date.
|
16
|
+
def needed?
|
17
|
+
!File.exist?(name) || out_of_date?(timestamp) || @application.options.build_all
|
18
|
+
end
|
19
|
+
|
20
|
+
# Time stamp for file task.
|
21
|
+
def timestamp
|
22
|
+
if File.exist?(name)
|
23
|
+
File.mtime(name.to_s)
|
24
|
+
else
|
25
|
+
Rake::LATE
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
# Are there any prerequisites with a later time than the given time stamp?
|
32
|
+
def out_of_date?(stamp)
|
33
|
+
all_prerequisite_tasks.any? { |prereq|
|
34
|
+
prereq_task = application[prereq, @scope]
|
35
|
+
if prereq_task.instance_of?(Rake::FileTask)
|
36
|
+
prereq_task.timestamp > stamp || @application.options.build_all
|
37
|
+
else
|
38
|
+
prereq_task.timestamp > stamp
|
39
|
+
end
|
40
|
+
}
|
41
|
+
end
|
42
|
+
|
43
|
+
# ----------------------------------------------------------------
|
44
|
+
# Task class methods.
|
45
|
+
#
|
46
|
+
class << self
|
47
|
+
# Apply the scope to the task name according to the rules for this kind
|
48
|
+
# of task. File based tasks ignore the scope when creating the name.
|
49
|
+
def scope_name(scope, task_name)
|
50
|
+
Rake.from_pathname(task_name)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,137 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require "rbconfig"
|
3
|
+
require "fileutils"
|
4
|
+
|
5
|
+
#--
|
6
|
+
# This a FileUtils extension that defines several additional commands to be
|
7
|
+
# added to the FileUtils utility functions.
|
8
|
+
module FileUtils
|
9
|
+
# Path to the currently running Ruby program
|
10
|
+
RUBY = ENV["RUBY"] || File.join(
|
11
|
+
RbConfig::CONFIG["bindir"],
|
12
|
+
RbConfig::CONFIG["ruby_install_name"] + RbConfig::CONFIG["EXEEXT"]).
|
13
|
+
sub(/.*\s.*/m, '"\&"')
|
14
|
+
|
15
|
+
# Run the system command +cmd+. If multiple arguments are given the command
|
16
|
+
# is run directly (without the shell, same semantics as Kernel::exec and
|
17
|
+
# Kernel::system).
|
18
|
+
#
|
19
|
+
# It is recommended you use the multiple argument form over interpolating
|
20
|
+
# user input for both usability and security reasons. With the multiple
|
21
|
+
# argument form you can easily process files with spaces or other shell
|
22
|
+
# reserved characters in them. With the multiple argument form your rake
|
23
|
+
# tasks are not vulnerable to users providing an argument like
|
24
|
+
# <code>; rm # -rf /</code>.
|
25
|
+
#
|
26
|
+
# If a block is given, upon command completion the block is called with an
|
27
|
+
# OK flag (true on a zero exit status) and a Process::Status object.
|
28
|
+
# Without a block a RuntimeError is raised when the command exits non-zero.
|
29
|
+
#
|
30
|
+
# Examples:
|
31
|
+
#
|
32
|
+
# sh 'ls -ltr'
|
33
|
+
#
|
34
|
+
# sh 'ls', 'file with spaces'
|
35
|
+
#
|
36
|
+
# # check exit status after command runs
|
37
|
+
# sh %{grep pattern file} do |ok, res|
|
38
|
+
# if !ok
|
39
|
+
# puts "pattern not found (status = #{res.exitstatus})"
|
40
|
+
# end
|
41
|
+
# end
|
42
|
+
#
|
43
|
+
def sh(*cmd, &block)
|
44
|
+
options = (Hash === cmd.last) ? cmd.pop : {}
|
45
|
+
shell_runner = block_given? ? block : create_shell_runner(cmd)
|
46
|
+
|
47
|
+
set_verbose_option(options)
|
48
|
+
verbose = options.delete :verbose
|
49
|
+
noop = options.delete(:noop) || Rake::FileUtilsExt.nowrite_flag
|
50
|
+
|
51
|
+
Rake.rake_output_message sh_show_command cmd if verbose
|
52
|
+
|
53
|
+
unless noop
|
54
|
+
res = (Hash === cmd.last) ? system(*cmd) : system(*cmd, options)
|
55
|
+
status = $?
|
56
|
+
status = Rake::PseudoStatus.new(1) if !res && status.nil?
|
57
|
+
shell_runner.call(res, status)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def create_shell_runner(cmd) # :nodoc:
|
62
|
+
show_command = sh_show_command cmd
|
63
|
+
show_command = show_command[0, 42] + "..." unless $trace
|
64
|
+
|
65
|
+
lambda do |ok, status|
|
66
|
+
ok or
|
67
|
+
fail "Command failed with status (#{status.exitstatus}): " +
|
68
|
+
"[#{show_command}]"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
private :create_shell_runner
|
72
|
+
|
73
|
+
def sh_show_command(cmd) # :nodoc:
|
74
|
+
cmd = cmd.dup
|
75
|
+
|
76
|
+
if Hash === cmd.first
|
77
|
+
env = cmd.first
|
78
|
+
env = env.map { |name, value| "#{name}=#{value}" }.join " "
|
79
|
+
cmd[0] = env
|
80
|
+
end
|
81
|
+
|
82
|
+
cmd.join " "
|
83
|
+
end
|
84
|
+
private :sh_show_command
|
85
|
+
|
86
|
+
def set_verbose_option(options) # :nodoc:
|
87
|
+
unless options.key? :verbose
|
88
|
+
options[:verbose] =
|
89
|
+
(Rake::FileUtilsExt.verbose_flag == Rake::FileUtilsExt::DEFAULT) ||
|
90
|
+
Rake::FileUtilsExt.verbose_flag
|
91
|
+
end
|
92
|
+
end
|
93
|
+
private :set_verbose_option
|
94
|
+
|
95
|
+
# Run a Ruby interpreter with the given arguments.
|
96
|
+
#
|
97
|
+
# Example:
|
98
|
+
# ruby %{-pe '$_.upcase!' <README}
|
99
|
+
#
|
100
|
+
def ruby(*args, &block)
|
101
|
+
options = (Hash === args.last) ? args.pop : {}
|
102
|
+
if args.length > 1
|
103
|
+
sh(*([RUBY] + args + [options]), &block)
|
104
|
+
else
|
105
|
+
sh("#{RUBY} #{args.first}", options, &block)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
LN_SUPPORTED = [true]
|
110
|
+
|
111
|
+
# Attempt to do a normal file link, but fall back to a copy if the link
|
112
|
+
# fails.
|
113
|
+
def safe_ln(*args)
|
114
|
+
if !LN_SUPPORTED[0]
|
115
|
+
cp(*args)
|
116
|
+
else
|
117
|
+
begin
|
118
|
+
ln(*args)
|
119
|
+
rescue StandardError, NotImplementedError
|
120
|
+
LN_SUPPORTED[0] = false
|
121
|
+
cp(*args)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
# Split a file path into individual directory names.
|
127
|
+
#
|
128
|
+
# Example:
|
129
|
+
# split_all("a/b/c") => ['a', 'b', 'c']
|
130
|
+
#
|
131
|
+
def split_all(path)
|
132
|
+
head, tail = File.split(path)
|
133
|
+
return [tail] if head == "." || tail == "/"
|
134
|
+
return [head, tail] if head == "/"
|
135
|
+
return split_all(head) + [tail]
|
136
|
+
end
|
137
|
+
end
|
@@ -0,0 +1,145 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require "rake/file_utils"
|
3
|
+
|
4
|
+
module Rake
|
5
|
+
#
|
6
|
+
# FileUtilsExt provides a custom version of the FileUtils methods
|
7
|
+
# that respond to the <tt>verbose</tt> and <tt>nowrite</tt>
|
8
|
+
# commands.
|
9
|
+
#
|
10
|
+
module FileUtilsExt
|
11
|
+
include FileUtils
|
12
|
+
|
13
|
+
class << self
|
14
|
+
attr_accessor :verbose_flag, :nowrite_flag
|
15
|
+
end
|
16
|
+
|
17
|
+
DEFAULT = Object.new
|
18
|
+
|
19
|
+
FileUtilsExt.verbose_flag = DEFAULT
|
20
|
+
FileUtilsExt.nowrite_flag = false
|
21
|
+
|
22
|
+
FileUtils.commands.each do |name|
|
23
|
+
opts = FileUtils.options_of name
|
24
|
+
default_options = []
|
25
|
+
if opts.include?("verbose")
|
26
|
+
default_options << ":verbose => FileUtilsExt.verbose_flag"
|
27
|
+
end
|
28
|
+
if opts.include?("noop")
|
29
|
+
default_options << ":noop => FileUtilsExt.nowrite_flag"
|
30
|
+
end
|
31
|
+
|
32
|
+
next if default_options.empty?
|
33
|
+
module_eval(<<-EOS, __FILE__, __LINE__ + 1)
|
34
|
+
def #{name}( *args, &block )
|
35
|
+
super(
|
36
|
+
*rake_merge_option(args,
|
37
|
+
#{default_options.join(', ')}
|
38
|
+
), &block)
|
39
|
+
end
|
40
|
+
EOS
|
41
|
+
end
|
42
|
+
|
43
|
+
# Get/set the verbose flag controlling output from the FileUtils
|
44
|
+
# utilities. If verbose is true, then the utility method is
|
45
|
+
# echoed to standard output.
|
46
|
+
#
|
47
|
+
# Examples:
|
48
|
+
# verbose # return the current value of the
|
49
|
+
# # verbose flag
|
50
|
+
# verbose(v) # set the verbose flag to _v_.
|
51
|
+
# verbose(v) { code } # Execute code with the verbose flag set
|
52
|
+
# # temporarily to _v_. Return to the
|
53
|
+
# # original value when code is done.
|
54
|
+
def verbose(value=nil)
|
55
|
+
oldvalue = FileUtilsExt.verbose_flag
|
56
|
+
FileUtilsExt.verbose_flag = value unless value.nil?
|
57
|
+
if block_given?
|
58
|
+
begin
|
59
|
+
yield
|
60
|
+
ensure
|
61
|
+
FileUtilsExt.verbose_flag = oldvalue
|
62
|
+
end
|
63
|
+
end
|
64
|
+
FileUtilsExt.verbose_flag
|
65
|
+
end
|
66
|
+
|
67
|
+
# Get/set the nowrite flag controlling output from the FileUtils
|
68
|
+
# utilities. If verbose is true, then the utility method is
|
69
|
+
# echoed to standard output.
|
70
|
+
#
|
71
|
+
# Examples:
|
72
|
+
# nowrite # return the current value of the
|
73
|
+
# # nowrite flag
|
74
|
+
# nowrite(v) # set the nowrite flag to _v_.
|
75
|
+
# nowrite(v) { code } # Execute code with the nowrite flag set
|
76
|
+
# # temporarily to _v_. Return to the
|
77
|
+
# # original value when code is done.
|
78
|
+
def nowrite(value=nil)
|
79
|
+
oldvalue = FileUtilsExt.nowrite_flag
|
80
|
+
FileUtilsExt.nowrite_flag = value unless value.nil?
|
81
|
+
if block_given?
|
82
|
+
begin
|
83
|
+
yield
|
84
|
+
ensure
|
85
|
+
FileUtilsExt.nowrite_flag = oldvalue
|
86
|
+
end
|
87
|
+
end
|
88
|
+
oldvalue
|
89
|
+
end
|
90
|
+
|
91
|
+
# Use this function to prevent potentially destructive ruby code
|
92
|
+
# from running when the :nowrite flag is set.
|
93
|
+
#
|
94
|
+
# Example:
|
95
|
+
#
|
96
|
+
# when_writing("Building Project") do
|
97
|
+
# project.build
|
98
|
+
# end
|
99
|
+
#
|
100
|
+
# The following code will build the project under normal
|
101
|
+
# conditions. If the nowrite(true) flag is set, then the example
|
102
|
+
# will print:
|
103
|
+
#
|
104
|
+
# DRYRUN: Building Project
|
105
|
+
#
|
106
|
+
# instead of actually building the project.
|
107
|
+
#
|
108
|
+
def when_writing(msg=nil)
|
109
|
+
if FileUtilsExt.nowrite_flag
|
110
|
+
$stderr.puts "DRYRUN: #{msg}" if msg
|
111
|
+
else
|
112
|
+
yield
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
# Merge the given options with the default values.
|
117
|
+
def rake_merge_option(args, defaults)
|
118
|
+
if Hash === args.last
|
119
|
+
defaults.update(args.last)
|
120
|
+
args.pop
|
121
|
+
end
|
122
|
+
args.push defaults
|
123
|
+
args
|
124
|
+
end
|
125
|
+
|
126
|
+
# Send the message to the default rake output (which is $stderr).
|
127
|
+
def rake_output_message(message)
|
128
|
+
$stderr.puts(message)
|
129
|
+
end
|
130
|
+
|
131
|
+
# Check that the options do not contain options not listed in
|
132
|
+
# +optdecl+. An ArgumentError exception is thrown if non-declared
|
133
|
+
# options are found.
|
134
|
+
def rake_check_options(options, *optdecl)
|
135
|
+
h = options.dup
|
136
|
+
optdecl.each do |name|
|
137
|
+
h.delete name
|
138
|
+
end
|
139
|
+
raise ArgumentError, "no such option: #{h.keys.join(' ')}" unless
|
140
|
+
h.empty?
|
141
|
+
end
|
142
|
+
|
143
|
+
extend self
|
144
|
+
end
|
145
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Rake
|
3
|
+
|
4
|
+
# InvocationChain tracks the chain of task invocations to detect
|
5
|
+
# circular dependencies.
|
6
|
+
class InvocationChain < LinkedList
|
7
|
+
|
8
|
+
# Is the invocation already in the chain?
|
9
|
+
def member?(invocation)
|
10
|
+
head == invocation || tail.member?(invocation)
|
11
|
+
end
|
12
|
+
|
13
|
+
# Append an invocation to the chain of invocations. It is an error
|
14
|
+
# if the invocation already listed.
|
15
|
+
def append(invocation)
|
16
|
+
if member?(invocation)
|
17
|
+
fail RuntimeError, "Circular dependency detected: #{to_s} => #{invocation}"
|
18
|
+
end
|
19
|
+
conj(invocation)
|
20
|
+
end
|
21
|
+
|
22
|
+
# Convert to string, ie: TOP => invocation => invocation
|
23
|
+
def to_s
|
24
|
+
"#{prefix}#{head}"
|
25
|
+
end
|
26
|
+
|
27
|
+
# Class level append.
|
28
|
+
def self.append(invocation, chain)
|
29
|
+
chain.append(invocation)
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def prefix
|
35
|
+
"#{tail} => "
|
36
|
+
end
|
37
|
+
|
38
|
+
# Null object for an empty chain.
|
39
|
+
class EmptyInvocationChain < LinkedList::EmptyLinkedList
|
40
|
+
@parent = InvocationChain
|
41
|
+
|
42
|
+
def member?(obj)
|
43
|
+
false
|
44
|
+
end
|
45
|
+
|
46
|
+
def append(invocation)
|
47
|
+
conj(invocation)
|
48
|
+
end
|
49
|
+
|
50
|
+
def to_s
|
51
|
+
"TOP"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
EMPTY = EmptyInvocationChain.new
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Rake
|
3
|
+
module InvocationExceptionMixin
|
4
|
+
# Return the invocation chain (list of Rake tasks) that were in
|
5
|
+
# effect when this exception was detected by rake. May be null if
|
6
|
+
# no tasks were active.
|
7
|
+
def chain
|
8
|
+
@rake_invocation_chain ||= nil
|
9
|
+
end
|
10
|
+
|
11
|
+
# Set the invocation chain in effect when this exception was
|
12
|
+
# detected.
|
13
|
+
def chain=(value)
|
14
|
+
@rake_invocation_chain = value
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Rake
|
3
|
+
# LateTime is a fake timestamp that occurs _after_ any other time value.
|
4
|
+
class LateTime
|
5
|
+
include Comparable
|
6
|
+
include Singleton
|
7
|
+
|
8
|
+
def <=>(other)
|
9
|
+
1
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_s
|
13
|
+
"<LATE TIME>"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
LATE = LateTime.instance
|
18
|
+
end
|
@@ -0,0 +1,112 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Rake
|
3
|
+
|
4
|
+
# Polylithic linked list structure used to implement several data
|
5
|
+
# structures in Rake.
|
6
|
+
class LinkedList
|
7
|
+
include Enumerable
|
8
|
+
attr_reader :head, :tail
|
9
|
+
|
10
|
+
# Polymorphically add a new element to the head of a list. The
|
11
|
+
# type of head node will be the same list type as the tail.
|
12
|
+
def conj(item)
|
13
|
+
self.class.cons(item, self)
|
14
|
+
end
|
15
|
+
|
16
|
+
# Is the list empty?
|
17
|
+
# .make guards against a list being empty making any instantiated LinkedList
|
18
|
+
# object not empty by default
|
19
|
+
# You should consider overriding this method if you implement your own .make method
|
20
|
+
def empty?
|
21
|
+
false
|
22
|
+
end
|
23
|
+
|
24
|
+
# Lists are structurally equivalent.
|
25
|
+
def ==(other)
|
26
|
+
current = self
|
27
|
+
while !current.empty? && !other.empty?
|
28
|
+
return false if current.head != other.head
|
29
|
+
current = current.tail
|
30
|
+
other = other.tail
|
31
|
+
end
|
32
|
+
current.empty? && other.empty?
|
33
|
+
end
|
34
|
+
|
35
|
+
# Convert to string: LL(item, item...)
|
36
|
+
def to_s
|
37
|
+
items = map(&:to_s).join(", ")
|
38
|
+
"LL(#{items})"
|
39
|
+
end
|
40
|
+
|
41
|
+
# Same as +to_s+, but with inspected items.
|
42
|
+
def inspect
|
43
|
+
items = map(&:inspect).join(", ")
|
44
|
+
"LL(#{items})"
|
45
|
+
end
|
46
|
+
|
47
|
+
# For each item in the list.
|
48
|
+
def each
|
49
|
+
current = self
|
50
|
+
while !current.empty?
|
51
|
+
yield(current.head)
|
52
|
+
current = current.tail
|
53
|
+
end
|
54
|
+
self
|
55
|
+
end
|
56
|
+
|
57
|
+
# Make a list out of the given arguments. This method is
|
58
|
+
# polymorphic
|
59
|
+
def self.make(*args)
|
60
|
+
# return an EmptyLinkedList if there are no arguments
|
61
|
+
return empty if !args || args.empty?
|
62
|
+
|
63
|
+
# build a LinkedList by starting at the tail and iterating
|
64
|
+
# through each argument
|
65
|
+
# inject takes an EmptyLinkedList to start
|
66
|
+
args.reverse.inject(empty) do |list, item|
|
67
|
+
list = cons(item, list)
|
68
|
+
list # return the newly created list for each item in the block
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
# Cons a new head onto the tail list.
|
73
|
+
def self.cons(head, tail)
|
74
|
+
new(head, tail)
|
75
|
+
end
|
76
|
+
|
77
|
+
# The standard empty list class for the given LinkedList class.
|
78
|
+
def self.empty
|
79
|
+
self::EMPTY
|
80
|
+
end
|
81
|
+
|
82
|
+
protected
|
83
|
+
|
84
|
+
def initialize(head, tail=EMPTY)
|
85
|
+
@head = head
|
86
|
+
@tail = tail
|
87
|
+
end
|
88
|
+
|
89
|
+
# Represent an empty list, using the Null Object Pattern.
|
90
|
+
#
|
91
|
+
# When inheriting from the LinkedList class, you should implement
|
92
|
+
# a type specific Empty class as well. Make sure you set the class
|
93
|
+
# instance variable @parent to the associated list class (this
|
94
|
+
# allows conj, cons and make to work polymorphically).
|
95
|
+
class EmptyLinkedList < LinkedList
|
96
|
+
@parent = LinkedList
|
97
|
+
|
98
|
+
def initialize
|
99
|
+
end
|
100
|
+
|
101
|
+
def empty?
|
102
|
+
true
|
103
|
+
end
|
104
|
+
|
105
|
+
def self.cons(head, tail)
|
106
|
+
@parent.cons(head, tail)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
EMPTY = EmptyLinkedList.new
|
111
|
+
end
|
112
|
+
end
|