Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completed First Ruby Asg - Ming #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 85 additions & 5 deletions username.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,93 @@
# Run `rspec /spec/username_spec.rb` to get started.

def format_name(first, last)
nil
user_name = ""
if !first.empty? && !last.empty?
user_name = first.strip[0].downcase + last.strip.split.join('')
else
return nil
end
user_name.downcase
end

def format_year
nil
def format_year(num)
if (num.to_s.length-1) == 3
return num.to_s[2,3]
else
return nil
end
end

def build_username
nil
def check_privilege(num = 0)
num = num.floor
if num == 0
return 'user'
elsif num == 1
return 'seller'
elsif num == 2
return 'manager'
elsif num >= 3
return 'admin'
end
end

def user_type_prefix (num)
if num == 0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm getting some code smell - you've already written this code before. How can you refactor this code by calling the check_privilege method?

return ""
elsif num == 1
return 'seller-'
elsif num == 2
return 'manager-'
elsif num == 3
return 'admin-'
end
end


def build_username(first, last, year, p_level = 0)
user_type_prefix(p_level).to_s + format_name(first, last) + year.to_s[-2, year.to_s.length-1]
end



$users = []

def generate_username (first, last, year, p_level = 0)

# put in variable for easy reference
new_username = format_name(first,last) + year.to_s[-2, year.to_s.length-1]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have a method to format the year!

new_username = format_name(first,last) + format_year(year)

# First check if our list is empty, if so, we just generate a new user
# with no incremental names attach, initialize the count, push into
# the user list and return that first registered name, ELSE =>
# From a list of users, check if new_username match any of the currently
# registered user name, if match, add '_count' to new_username, update count
# and then push into increment_name list associated with that matched user
# and then return that incremented name as output
if $users[0] == nil

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great solution!

user = {
:name => new_username,
:increment_names => [],
:count => 1,
}
$users.push(user)
return new_username
else
$users.each do |user|
if user[:name] == (new_username)
new_username = new_username + "_" + user[:count].to_s
user[:count] = user[:count].to_i + 1
user[:increment_names].push(new_username)
return new_username
end
end
# if by any chance our list does not contain any duplicate names
# we create a fresh hash, push it to the list and return that name
user = {
:name => new_username,
:increment_names => [],
:count => 1,
}
$users.push(user)
return new_username
end
end
18 changes: 12 additions & 6 deletions warmup.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
# Run `rspec spec/the_warmup_spec.rb` to get started.

def say_hello
"hi"
"hello"
end

def scream(message)
message
message.upcase
message = message.upcase + "!"
return message
end

def first_char
"z"
def first_char(word)
word[0].downcase
end

def polly_wanna
"crackercrackercracker"
def polly_wanna(word)
word*3
end

def after_you
'no, after you'
end