Posted by: americarocksdude | February 18, 2014

RUBY RIFFBOOK — A Beginning ruby program for guitarplayers to teach menu, if / else, case and operators — RUBY PROGRAMMING


This is a very very simple program designed for guitar players to save their links, tabs, songs, notes. The program has tons of room for improvement so please feel free to add on to it as you wish. This program is a great example of what you can do with information fresh from most beginning ruby tutorials. All of this syntax should be familiar, and using the menu of this program you should be able to skeleton an endless of amount of useful programs from this format. Feel free to leave comments or questions!

def space
 puts ” “
 puts ” “
end

# MAIN MENU

def main_menu
 space
 space
 space
 puts ”          WELCOME TO RIFFBOOK          “
 puts ”        2014 — AMERICAROCKSDUDE     “
 space
 puts ” PLEASE MAKE A SELECTION [ 1 – 5 ]     “
 space
 puts ” [1] ————– TAB ARCHIVE        “
 puts ” [2] ————– CHORD LISTING      “
 puts ” [3] ————– SONG WRITING       “
 puts ” [4] ————– MUSICIAN NOTEPAD   “
 puts ” [5] ————– HELPFUL LINKS      “
 puts ” [6] ————– QUIT RIFFBOOK      “
 space
 space
 
 answer = gets.chomp.to_i
 
 case answer

 when 1
  space
  archive

 when 2
  space
  listing

 when 3
  space
  creation

 when 4
  space
  notepad

 when 5
  space
  links
 when 6
  exit
end
end

def archive
 space
 puts ” ——————— TAB ARCHIVE ———————–“
 puts “Would you like to [write] to the tab archive, [read] it, or [delete] it?”
 space
 answer = gets.chomp.downcase
 space
 case answer
 when “read”
  space
  File.open(“RIFFBOOK-DATABASE.txt”, “r”) do |f|
  contents = f.read
  puts contents
  end
  space
  space
  puts “Press enter to return to the main menu”
  answer = gets.chomp
  space
  main_menu
 when “write”
  writing = true
  while writing
  puts “Please enter the name of the song you wish you add to the database”
  space
  name = gets.chomp.downcase
  space
  puts “Please enter the link for #{name} tabs.”
  space
  link = gets.chomp.downcase
  space
  name_link = name + ” : ” + link
  File.open(“RIFFBOOK-DATABASE”+ “.txt”,’a’) do |f|
  f.puts name_link
  space
  puts “Would you like to continue working in the songs? [y] or [n]”
  space
  answer = gets.chomp.downcase
  space
   if answer == ‘n’
    writing = false
    main_menu
   else
    archive
end
end
end
end
def listing
 space
 puts ” ——————— CHORD LISTINGS ———————–“
 puts “Would you like to [write] a chord listing or [read] the list?”
 space
 answer = gets.chomp.downcase
 space
 case answer
 when “read”
  space
  File.open(“RIFFBOOK-CHORDBOOK.txt”, “r”) do |f|
  contents = f.read
  puts contents
  end
  space
  space
  puts “Press enter to return to the main menu”
  answer = gets.chomp
  space
  main_menu
 when “write”
 writing = true
 while writing
 space
 space
 puts “Please enter the notation of the chord”
 space
 chord_name = gets.chomp
 space
 puts “Please enter the 6 digit chord pattern”
 space
 puts “EXAMPLE XX0032 OR 0032010 OR 777987  “
 space
 chord = gets.chomp
 chordlisting = chord_name + ” : ” + chord
  File.open(“RIFFBOOK-CHORDBOOK”+ “.txt”,’a’) do |f|
  f.puts chordlisting
  end
  space
  puts “Would you like to continue working in chords? [y] or [n]”
  space
  answer = gets.chomp.downcase
  space
   if answer == ‘n’
    writing = false
    main_menu
   else
    listing
end
end
 end
end
end

def creation
 space
 puts ” ———————- SONG WRITING ————————“
 puts “Would you like to [write] a new song [read] all song entries?”
 space
 answer = gets.chomp.downcase
 
 case answer
 
 when “write”
  writing = true
  while writing
  space
  puts “Please enter a title for the song..”
  space
  title = gets.chomp.downcase
  space
  puts “Please enter the lyrics of the song..”
  space
  lyrics = gets.chomp.downcase
  space
  puts “Please enter the chords of the song..”
  space
  chords = gets.chomp.downcase
  time = Time.now.to_s
  File.open(“RIFFBOOK-WRITING”+ “.txt”,’a’) do |f|
  f.puts space
  f.puts “Created at : ” + time
  f.puts “Title : ” + title
  f.puts “Lyrics : ” + lyrics
  f.puts “Chords : ” + chords
  f.puts space
  end
  puts “Would you like to continue working in songs? [y] or [n]”
  space
  answer = gets.chomp.downcase
  space
   if answer == ‘n’
    writing = false
    main_menu
   else
    creation
   end
   end
 
 when “read”
  File.open(“RIFFBOOK-WRITING.txt”, “r”) do |f|
  contents = f.read
  puts contents
  end
  space
  puts “PRESS ENTER TO RETURN TO MAIN MENU”
  answer = gets.chomp
  space
 end
  main_menu
end

def notepad
 space
 puts ” ——————— NOTEPAD ———————–“
 puts “You can [write] a new note or [read] existing notes”
 space
 answer = gets.chomp.downcase
 space
 case answer

 when “write”
 space
 puts “Please enter your notes, press enter to save”
 space
 notes = gets.chomp.downcase
 time = Time.now.to_s
 File.open(“RIFFBOOK-NOTES”+ “.txt”,’a’) do |f|
 f.puts space
 f.puts “Note created at ” +  time
 f.puts notes
 f.puts space
 end
 puts “Would you like to continue working in notes? [y] or [n].”
 space
 answer = gets.chomp.downcase
 space
 if answer == “y”
  notepad
 else
  main_menu
 end
 when “read”
 File.open(“RIFFBOOK-NOTES”+ “.txt”,’r’) do |f|
 contents = f.read
 space
 puts contents
 end
 space
 puts “PRESS ENTER TO RETURN TO MAIN MENU”
 answer = gets.chomp
 space
 main_menu
end
end

def links
 space
 puts ” ——————— LINKS ———————“
 puts “Would you like to [write] or [read] current links?”
 space
 answer = gets.chomp.downcase
 space
 case answer
  when “write”
   space
   puts “Please enter the name of the website..”
   space
   name = gets.chomp.downcase
   space
   puts “Please enter the url of the website..”
   space
   link = gets.chomp
   space
   the_link = name + ” : ” + link
   File::open(“RIFFBOOK-LINKS.txt”,”a”) do |f|
   f.puts space
   f.puts the_link
   end
   space
   puts “Would you like to continue working in links? [y] or [n].”
   space
   answer = gets.chomp
   space
    if answer == “y”
     links
    else
     main_menu
   end
  when “read”
   space
   File.open(“RIFFBOOK-LINKS.txt”,”r”) do |f|
   contents = f.read
   puts contents
   end
   space
   puts “PRESS ENTER TO RETURN TO THE MAIN MENU”
   answer = gets.chomp
   space
   main_menu
end
end
end
 
main_menu

 

Advertisements

Responses

  1. I would suggest copy and pasting the above code into a text document. sorry about the spacing, when i pasted it in it auto-formated! Run this program through console using “ruby (filename.rb)” without the () and changing filename to whatever the file you saved the code into is called. make sure you change the text document to a ‘.rb’ file AND if needed make it executable chmod -x filename.rb

    🙂


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: