A080437f9a305a2f2a8ce517b304ad00

This is the first program I've ever written in Ruby. It's a personal utility to create memos for a Rockbox (see rockbox.org) application that reads memos from a text file named .memo and displays them in a calendar. What this script does is prompts the user for the date of the memo and the actual memo. It then converts the user data to the utility readable version and inserts it into the text file.

The format for the utility to read the memos is each memo is separated on a different line. the first 2 digits are the day of the month, the second two are the month number (1 for jan, 2 for feb, etc), the next four are the year, the next digit is the day of the week (0 for monday, 1 for tuesday, 6 for sunday) and the last digit is the kind of memo (0 for weekly, 1 for monthly, 2 for yearly, 3 for one time). Then the memo follows on the rest of the line.

Here is the code. I don't think it is very efficient or pretty. Please help. I'm still learning Ruby so if you see a better way to do it this could help me learn new concepts. Thanks!!

require 'date'

#Get data from user for memo
puts 'What kind of memo would you like?'
puts '0. Weekly'
puts '1. Monthly'
puts '2. Yearly'
puts '3. One Time'
memoType = gets()
memoType = memoType.chomp! #Removes \n
puts 'What day would this be?'
puts 'year/month/day Ex. 1992/3/24'
memoDate = gets()
new_date = Date.parse(memoDate)
#Add 0 to beginning of day and month if they are one digit.
if new_date.day >= 10
  day = new_date.day
else
  day = "0" + (new_date.day.to_s)
end
if new_date.mon >= 10
  mon = new_date.mon
else
  mon = "0" + (new_date.mon.to_s)
end
puts 'What is the memo?'
memo = gets()
#Read in existing file and add new memo at the top of the file
lines = IO.readlines(".memo")
lines.insert(0, "#{day}#{mon}#{new_date.year}#{new_date.cwday-1}#{memoType}#{memo}")
f = File.new(".memo", "w");
lines.each{|line| f.puts(line)}
f.close




--------------------- END RUBY CODE ------------------------------

Example of text file .memo :

3005201063Matts party 1:30
2805201043Care for dog
2705201033Call Sherry

Refactorings

No refactoring yet !

7855792dbc5f3b4c365344314e2b1ad6

arvanasse

June 1, 2010, June 01, 2010 14:42, permalink

3 ratings. Login to rate!

Not a bad first attempt. I'll offer just a few ideas that you might want to look into:
- You can create an array of strings using the syntax %w{string1 string2 string3}. You might do this to control the "memo types".
- Ruby Date and Time classes have a strftime method that is very similar to C/C++/C#. You'd be better off making use of that for formatting the dates.
- Is it necessary to insert the new memo at the top of the file? If not you could open the file using the "a+" mode which gives you the benefit of creating the file if it does not exist and going to the end for append. You can combine that with passing a block to the '#open' method and it will automatically close the file for you.
- Someone eventually will suggest you wrap this up in a class. They'll be right.

Taken together you might end up like this:

require 'date'

# Creating this array in the correct order allows you to use the index as the suggested value.
MemoTypes = %{Weekly Monthly Yearly One-Time}.freeze

#Get data from user for memo
puts 'What kind of memo would you like?'
MemoTypes.each_with_index{|memo_type, idx| puts "#{idx}. #{memo_type}"}

memoType = gets()
memoType = memoType.chomp! #Removes \n

puts 'What day would this be?'
puts 'year/month/day Ex. 1992/3/24'
memoDate = gets()
new_date = Date.parse(memoDate)

puts 'What is the memo?'
memo = gets()
#Read in existing file and add new memo at the top of the file

File.open(".memo", "a+") do |f|
  f.puts  "#{new_date.strftime('%d%m%Y')}#{new_date.cwday-1}#{memoType}#{memo}"
end




--------------------- END RUBY CODE ------------------------------

Example of text file .memo :

3005201063Matts party 1:30
2805201043Care for dog
2705201033Call Sherry
D41d8cd98f00b204e9800998ecf8427e

bob

June 1, 2010, June 01, 2010 16:45, permalink

2 ratings. Login to rate!

You don't need parenthesis when calling a function with no arguments. So "gets()" can be written as "gets".

Ee0505bbd355292778077fb662c88f13

Fu86

June 16, 2010, June 16, 2010 20:56, permalink

1 rating. Login to rate!

The "chomp!" is a little bit complicated. You can simplify this.

memoType = gets.chomp #Removes \n
new_date = Date.parse gets

Your refactoring





Format Copy from initial code

or Cancel