It so happened that before I had a little programming experience in Java and PHP. Therefore, I easily switched to Ruby. Of course, I often had to refer to the literature, as well as to the wonderful utility
ri (or the more convenient analogue of
fxri ).
Features of ruby language came to my liking. (I really could not say that after switching from java to php)
The ruby language is rich in syntactic sugar. A simple loop here can be written in a variety of ways, and in many cases it improves the readability of the program. In other matters, the language itself is positioned as a programming language, which absorbed the best moments of other programming languages.
I would like to present you a simple console utility, designed to work with the task list. It is very simple, and if someone likes this article,
I will continue to increase the functionality and give detailed explanations to the program code.
To run this utility you just need to write in the console:
$ ruby ./rtodo.rb
The program consists of three files:
system.rb - file containing the implementation of the System class
rtodo.rb - the main executable file
list.dat is our task repository
')
Instructions for work (in the examples):
add Complete script for my web-page (add task)
list (print a list of all tasks - each task has its own number)
remove 1 (delete task number 1)
First I want to show the source code without comments. After this, I will write in detail what each line does.
rtodo.rb

System.rb

Let's start learning from the main file - rtodo.rb
Load the file containing the implementation of the System class.
load 'system.rb'
System class is responsible for working with tasks (add / remove / show)
create an object of class System
system = System.new
welcome our user
puts "> Welcome to R-todo v0.1, type 'help' to get info"
we initialize the string variable, this is where the user will be stored
user_input = ""
using the
while
construct, we process everything the user enters, until the user enters 'quit', this will mean that the program should end immediately
while user_input != "quit"
we bring in variable that entered our user in the console
user_input = gets.chomp
convert the string that the user entered into the array. Each word is a separate element of the array.
formatted_user_input = user_input.split
then we will go through each element of the hash array of keywords. You are probably surprised. You will find a description of this variable in the system.rb file — for now, just know that this hash array contains constructions of the form 'keyword' => 'called function'
system.keywords.each do |keyword,method|
if the first word in the line that the user entered corresponds to a keyword, then ...
if formatted_user_input[0] == keyword
we initialize an array in which the line which was entered by the user will be stored. Only without the first word, because it is the key, and serves exclusively for the work of the program
buffer = []
this string fills the array with words
1.upto(formatted_user_input.index(formatted_user_input.last)) { |index| buffer.push(formatted_user_input.at(index))}
and then combines the words of the array into a single string, using the space character as a separator
string_user_input = buffer.join(" ")
This line below should be paid special attention.
In addition to syntactic sugar, there is a dangerous eval method here, you can read more about it in any book on php / ruby. The fact is that the task_list method does not provide arguments, so if the user entered the list command, the argument of the eval function would be of the same type, and if the user entered some other command, then the argument would be quite standard for our System class.
keyword == "list" ? eval("system.#{method}") : eval("system.#{method}(string_user_input)")
end
end
end
puts "> R-todo was aborted by user"
Go to the file System.rb
I want to remind you, this file is directly responsible for working with the task list.
class System
object variables
attr_reader :keywords, :buffer
constructor method, this method will always be called when creating an object of the System class
def initialize
And so, for a start we initialize the hash array of the @keywords object variable.
this array will contain keywords and method names
in other words, when the user enters 'add', the 'task_add' method will be called
@keywords = { 'add' => 'task_add', 'remove' => 'task_remove', 'list' => 'task_list'}
create an array variable. Here we will put all current (existing tasks)
@buffer = []
I will explain in more detail.
All tasks of the user are stored in the buffer. As soon as the user deletes any task, it is first deleted from the buffer, then the entire buffer is written to the file. In other words - every time we re-write the contents of our file. For this program, this is permissible, but in applications that work with huge files, this method can affect performance.
list.dat
file contains a list of tasks
if the file containing the task list exists
if File.exist?('list.dat')
we initialize the list_file variable containing our list.dat file
and transfer the data from the file to our buffer
list_file = File.new('list.dat','r')
list_file.each_line do |line|
@buffer << line
end
if this file does not exist, then ...
else
create new file
list_file = File.new("list.dat","w+")
puts "System files was created"
end
close the file (save)
list_file.close
end
the method adds a new task, as an argument - the text of the task
def task_add(task)
we put the text of the task in the buffer
@buffer << task
puts "> task added to list (#{task})"
task_write_to_file
call the method that writes the entire buffer to a file
task_list
call the method showing the current task list (updated)
end
the method deletes the task, the task number is used as an argument
def task_remove(number)
if the argument is incorrect
if number.to_i > (@buffer.index(@buffer.last))
puts "> Please write a correct task index"
if right
else
puts "> Task #{number} removed"
delete the task from the buffer
@buffer.delete_at(number.to_i)
Call the method that writes the entire buffer to a file.
task_write_to_file
end
call the method showing the current task list (updated)
task_list
end
method showing current task list
def task_list
puts "********* #{@buffer.index(@buffer.last) + 1} item's *********\n"
pass through each element of the buffer
@buffer.each do |task|
puts "\t #{@buffer.index(task)} - #{task}"
end
puts "\n********* #{@buffer.index(@buffer.last) + 1} item's *********"
end
a method that writes the entire buffer to a file
def task_write_to_file
open the file, if you specify 'w' as the second argument, then each time the file will be written again, unlike the alternative argument 'a'
file = File.new('list.dat', 'w')
pass through each element of the buffer
@buffer.each do |task|
write current buffer element to file
file.puts task
end
close the file (save)
file.close
end
end
I want to draw your attention to the fact that this is a very raw program. Including in terms of security. I did not pay much attention to checking what the user enters. (and meanwhile, this is the most important part) If you like this material, then I will continue to increase the functionality.Download the archive with the program [Zip, 1.2 kb]
Version with a normal backlight in my blog