patternrubyMinor
Usertool to Edit Forum Members from Desktop
Viewed 0 times
editdesktopusertoolforummembersfrom
Problem
I've created a Forum website tool for admins to add, edit, and delete users from their desktop, saving to a file, and uploading the file to the database, the program is not completed I'm posting it here for review thus far
(People I'm creating it for would prefer me not having access to database, I understand I would be using
What the program does is simple:
delete users.
and if the option isn't found it exits the program
The edit user section hasn't been setup yet, still working out the kinks in
So what I'm looking for is:
entire file and not just the user entered, is this because I'm deleting
the entire section and not just the users input?
that A LOT of websites don't support
right off the bat that should be changed, can my syntax be updated or more developed?
Source Code:
```
#!/usr/bin/env ruby
require 'yaml'
require 'open-uri'
def menu
print "Welcome to
(People I'm creating it for would prefer me not having access to database, I understand I would be using
activerecord model/structure if I where saving directly to the database.)What the program does is simple:
- It prompts the user for which he/she would like to do via
add, edit,
delete users.
- The program then runs through a case statement of available options
and if the option isn't found it exits the program
- In the
add_usermethod the program saves the input of the user into a YAML file, and prompts the user for more users, if none follow exits the program.
- During the
delete_usermethod, the program does the same as adding except deletes the users from the YAML file there is a bug where the program deletes the entire YAML file, not just the edited users
- In the
edit_userthe program will edit various elements of the hash inside theYAMLfile and save over the old element of the file. Not ready yet will post follow up later
The edit user section hasn't been setup yet, still working out the kinks in
delete_user method.So what I'm looking for is:
- A possible explanation of why the
delete_usermethod wipes the
entire file and not just the user entered, is this because I'm deleting
the entire section and not just the users input?
- Would using a
JSONfile be better then using aYAMLfile, I'm aware
that A LOT of websites don't support
YAML's so would it be a good idea to convert the require 'yaml' to a require 'json' and regroup from there? - Any possible areas of improvement? Meaning do you notice anything
right off the bat that should be changed, can my syntax be updated or more developed?
Source Code:
```
#!/usr/bin/env ruby
require 'yaml'
require 'open-uri'
def menu
print "Welcome to
Solution
prompt functionSo many times you do:
puts "Please enter Username:" # Or something else instead of Username.
username = gets.chompIn short you print and then read input, what about a function:
def prompt(message)
puts message
gets.chomp
endYou can reuse it and save a lot of repetition.
Functional Programming -- No mutation
Ruby leans towards the Function Programming paradigm, so you should stick to it.
One thing is avoiding mutation:
data = {username: username}
...
data[:email_address] = email
...
data[:member_status] = statusYou modify data piece by piece, instead do:
...
...
data = {username: username, email_address: email, member_status: status}It also looks more organized to me.
In fact you may write:
data = { username: prompt("The username? "),
email_address: prompt("The email address? ")
member_status: prompt("The member status? ") }And make the code shorter and more readable.
Code Snippets
puts "Please enter Username:" # Or something else instead of Username.
username = gets.chompdef prompt(message)
puts message
gets.chomp
enddata = {username: username}
...
data[:email_address] = email
...
data[:member_status] = status...
...
data = {username: username, email_address: email, member_status: status}data = { username: prompt("The username? "),
email_address: prompt("The email address? ")
member_status: prompt("The member status? ") }Context
StackExchange Code Review Q#109874, answer score: 3
Revisions (0)
No revisions yet.