./src/_compilation/abs in elonmusk-1.0.82 vs ./src/_compilation/abs in elonmusk-1.0.87
- old
+ new
@@ -1,14 +1,27 @@
#!/usr/bin/crystal
-def delete(arg)
- `find -name #{arg}`.each_line do |i|
- File.delete(i)
- end
+CORE_UTIL_STRING = "core_utils"
+
+def prepend_each(arg)
+ STDIN.each_line do |line|
+ puts arg + line
+ end
end
+def append_each(arg)
+ STDIN.each_line do |line|
+ puts line + arg
+ end
+end
+def delete(arg)
+ `find -name #{arg}`.each_line do |i|
+ File.delete(i)
+ end
+end
+
def nth_word(arg)
i = arg.to_u64 - 1
STDIN.each_line do |line|
puts line.split[i] rescue ""
end
@@ -18,15 +31,15 @@
STDIN.each_line do |line|
puts line.strip
end
end
-def lines()
+def lines(folder = ".")
if !STDIN.tty?
- puts STDIN.gets_to_end.count("\n")
+ puts STDIN.gets_to_end.count('\n')
else
- res = `find .`.count("\n") - 1
+ res = `find #{folder}`.count('\n') - 1
puts res
end
end
def args(arg1)
@@ -153,29 +166,29 @@
end
def gsubip(arg1, arg2, arg3)
regex = Regex.new(arg1)
text = File.read(arg3).gsub(/#{arg1}/m, arg2)
- File.open(t = File.tempname("coreutils", "_tmp"), "w", File.info(arg3).permissions) do |h|
+ File.open(t = File.tempname(CORE_UTIL_STRING, "_tmp"), "w", File.info(arg3).permissions) do |h|
h.print text
end
begin
File.rename t, arg3
rescue
- rm t
+ File.delete t
end
end
def rip(arg1, arg2, arg3)
text = File.read(arg3).gsub(arg1, arg2)
- File.open(t = File.tempname("coreutils", "_tmp"), "w", File.info(arg3).permissions) do |h|
+ File.open(t = File.tempname(CORE_UTIL_STRING, "_tmp"), "w", File.info(arg3).permissions) do |h|
h.print text
end
begin
File.rename t, arg3
rescue
- rm t
+ File.delete t
end
end
def gsub(arg1, arg2)
print STDIN.gets_to_end.gsub(/#{arg1}/m, arg2)
@@ -195,41 +208,41 @@
def swap(file1, file2)
[file1, file2].each do |f|
File.exists?(f) || abort("No file named #{f.dump}")
end
- t = File.tempname("coreutils", "_tmp")
+ t = File.tempname(CORE_UTIL_STRING, "_tmp")
File.rename file1, t
File.rename file2, file1
File.rename t, file2
end
def prepend(file)
file_data = File.read(file)
new_data = STDIN.gets_to_end
- File.open(t = File.tempname("coreutils", "_tmp"), "w", File.info(file).permissions) do |h|
+ File.open(t = File.tempname(CORE_UTIL_STRING, "_tmp"), "w", File.info(file).permissions) do |h|
h << new_data + file_data
end
begin
File.rename t, file
rescue
- rm t
+ File.delete t
end
end
def append(file)
file_data = File.read(file)
new_data = STDIN.gets_to_end
- File.open(t = File.tempname("coreutils", "_tmp"), "w", File.info(file).permissions) do |h|
+ File.open(t = File.tempname(CORE_UTIL_STRING, "_tmp"), "w", File.info(file).permissions) do |h|
h << file_data + new_data
end
begin
File.rename t, file
rescue
- rm t
+ File.delete t
end
end
class Funcs
def self.argument_data(program, usage, long_desc)
@@ -256,16 +269,22 @@
puts text
end
end
-def main()
- if ARGV.size != 0
+def main
+ size = ARGV.size
+
+ if size > 0
Funcs.argument_data("abs", "[abs] [arguments]", "Abs gets the absolute value of an integer\n\nThe absolute value of a number is the value of that number without its sign\n\necho -300 | abs\n\n=> 300\n\necho 200 | abs\n\n=> 200 \n\n(echo -2; echo -10) | sub | abs\n\n=> 12\n\nThe last one would do -2 - -10, the result would be -12, and then the absolute value would be computed, and so the result would be 12.")
exit 1
end
- abs
+ begin
+ abs
+ rescue e : Exception
+ puts e
+ end
end
main