Improve a piece of code

Hi,

With the following Ruby script :

--8<--
class Testing

   def initialize
     # Start with setting your name
     your_name
   end

   def your_name
     # Get your name
     puts "(Type 'quit' to exit)"
     print 'Please your name? '
     @name = gets.chomp.capitalize

     @name = 'Guest' if @name.empty?
     byebye if @name == 'Quit'

     play
   end

   def byebye
     @name = 'Guest' if @name == 'Quit'
     puts "\nGoodbye #{@name}.\n\n"

     exit
   end

   def play
     puts "Let's play #{@name}!"
   end

end

Testing.new

-->8--

How can avoid the 2 assignments of @name = 'Guest' ?
Is it possible refactor this piece of code smaller ?

Thank you very much for your replies.

/Nathan

I think you can simply set @name = ‘Guest’ in the constructor that way @name has a default value of ‘Guest'

···

On 24 Nov 2016, at 12:30 PM, Nathan Guilty <ruby@e-solutions.re> wrote:

Hi,

With the following Ruby script :

--8<--
class Testing

def initialize
   # Start with setting your name
   your_name
end

def your_name
   # Get your name
   puts "(Type 'quit' to exit)"
   print 'Please your name? '
   @name = gets.chomp.capitalize

   @name = 'Guest' if @name.empty?
   byebye if @name == 'Quit'

   play
end

def byebye
   @name = 'Guest' if @name == 'Quit'
   puts "\nGoodbye #{@name}.\n\n"

   exit
end

def play
   puts "Let's play #{@name}!"
end

end

Testing.new

-->8--

How can avoid the 2 assignments of @name = 'Guest' ?
Is it possible refactor this piece of code smaller ?

Thank you very much for your replies.

/Nathan

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

A refactored example could be :

class Testing
  attr_accessor :name
  
def initialize
   # Start with setting your name
   your_name
   @name = 'Guest'
end

def your_name
   # Get your name
   puts "(Type 'quit' to exit)"
   print 'Please your name? '
   @name = gets.chomp.capitalize
   byebye if name == 'Quit'

   play
end

def byebye
   puts "\nGoodbye #{name}.\n\n"

   exit
end

def play
   puts "Let's play #{name}!"
end

end

···

On 24 Nov 2016, at 12:30 PM, Nathan Guilty <ruby@e-solutions.re> wrote:

Hi,

With the following Ruby script :

--8<--
class Testing

def initialize
   # Start with setting your name
   your_name
end

def your_name
   # Get your name
   puts "(Type 'quit' to exit)"
   print 'Please your name? '
   @name = gets.chomp.capitalize

   @name = 'Guest' if @name.empty?
   byebye if @name == 'Quit'

   play
end

def byebye
   @name = 'Guest' if @name == 'Quit'
   puts "\nGoodbye #{@name}.\n\n"

   exit
end

def play
   puts "Let's play #{@name}!"
end

end

Testing.new

-->8--

How can avoid the 2 assignments of @name = 'Guest' ?
Is it possible refactor this piece of code smaller ?

Thank you very much for your replies.

/Nathan

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Quick one, setting @name should come first, I think I missed that out

class Testing
  attr_accessor :name
  
def initialize
   # Start with setting your name
   @name = 'Guest'
   your_name
end

def your_name
   # Get your name
   puts "(Type 'quit' to exit)"
   print 'Please your name? '
   @name = gets.chomp.capitalize
   byebye if name == 'Quit'

   play
end

def byebye
   puts "\nGoodbye #{name}.\n\n"

   exit
end

def play
   puts "Let's play #{name}!"
end
end

···

On 24 Nov 2016, at 12:30 PM, Nathan Guilty <ruby@e-solutions.re> wrote:

Hi,

With the following Ruby script :

--8<--
class Testing

def initialize
   # Start with setting your name
   your_name
end

def your_name
   # Get your name
   puts "(Type 'quit' to exit)"
   print 'Please your name? '
   @name = gets.chomp.capitalize

   @name = 'Guest' if @name.empty?
   byebye if @name == 'Quit'

   play
end

def byebye
   @name = 'Guest' if @name == 'Quit'
   puts "\nGoodbye #{@name}.\n\n"

   exit
end

def play
   puts "Let's play #{@name}!"
end

end

Testing.new

-->8--

How can avoid the 2 assignments of @name = 'Guest' ?
Is it possible refactor this piece of code smaller ?

Thank you very much for your replies.

/Nathan

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

This doesn't work :

1/ Set the name
Case 1 : If the name is empty so, i need to have "Let's play Guest!"
Case 2 : If the name is quit, i need to have "Goodbye Guest."
Case 3 : A real player name, i need to have "Let's play #{@name}"

:slight_smile:

/Nathan

···

Le 2016-11-24 15:48, Damian Simon Peter a écrit :

Quick one, setting @name should come first, I think I missed that out

class Testing
attr_accessor :name

def initialize
# Start with setting your name
@name = 'Guest'
your_name
end

def your_name
# Get your name
puts "(Type 'quit' to exit)"
print 'Please your name? '
@name = gets.chomp.capitalize
byebye if name == 'Quit'

play
end

def byebye
puts "\nGoodbye #{name}.\n\n"

exit
end

def play
puts "Let's play #{name}!"
end

On 24 Nov 2016, at 12:30 PM, Nathan Guilty <ruby@e-solutions.re> >> wrote:

Hi,

With the following Ruby script :

--8<--
class Testing

def initialize
# Start with setting your name
your_name
end

def your_name
# Get your name
puts "(Type 'quit' to exit)"
print 'Please your name? '
@name = gets.chomp.capitalize

@name = 'Guest' if @name.empty?
byebye if @name == 'Quit'

play
end

def byebye
@name = 'Guest' if @name == 'Quit'
puts "\nGoodbye #{@name}.\n\n"

exit
end

def play
puts "Let's play #{@name}!"
end

end

Testing.new

-->8--

How can avoid the 2 assignments of @name = 'Guest' ?
Is it possible refactor this piece of code smaller ?

Thank you very much for your replies.

/Nathan

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

​My brief sketch of an algorithm would be:

1. initialise the name to "Guest"
2. read a word from the user (into a local variable, not @name)
  2.a) if it's "Quit", go to end
  2.b) otherwise if it's not empty, assign it to @name
3. play​

···

On 24 November 2016 at 21:50, Nathan Guilty <ruby@e-solutions.re> wrote:

This doesn't work :

1/ Set the name
Case 1 : If the name is empty so, i need to have "Let's play Guest!"
Case 2 : If the name is quit, i need to have "Goodbye Guest."
Case 3 : A real player name, i need to have "Let's play #{@name}"

:slight_smile:

--
  Matthew Kerwin
  http://matthew.kerwin.net.au/

Looks like you might have a problem if try_a_name is empty…. in that case:

@name = try_a_name unless try_a_name == 'Quit' || try_a_name.empty?

will not set @name. unless you are ok with the default name of “Guest”… then all is well…

Max

···

On Nov 24, 2016, at 5:23 AM, Nathan Guilty <ruby@e-solutions.re> wrote:

Thank you very much Matthew, i was wrong from the beginning :slight_smile:

So here it is :

--8<--

class Testing

def initialize
   # Start with setting your name
   # Case 1 : name is empty => "Let's play Guest!"
   # Case 2 : name is a real name => "Let's play your_name"
   # Case 3 : name is 'quit' => "Goodbye Guest." & exit game

   @name = 'Guest'
   your_name
end

def your_name
   # Get your name
   puts "(Type 'quit' to exit)"
   print 'Please your name? '
   try_a_name = gets.chomp.capitalize

   @name = try_a_name unless try_a_name == 'Quit' || try_a_name.empty?
   byebye if try_a_name == 'Quit'

   play
end

def byebye
   puts "\nGoodbye #{@name}.\n\n"

   exit
end

def play
   puts "Let's play #{@name}!"
end

end

Testing.new

-->8---

/Nathan

Le 2016-11-24 16:00, Matthew Kerwin a écrit :

On 24 November 2016 at 21:50, Nathan Guilty <ruby@e-solutions.re> wrote:

This doesn't work :
1/ Set the name
Case 1 : If the name is empty so, i need to have "Let's play Guest!"
Case 2 : If the name is quit, i need to have "Goodbye Guest."
Case 3 : A real player name, i need to have "Let's play #{@name}"
:slight_smile:

​My brief sketch of an algorithm would be:
1. initialise the name to "Guest"
2. read a word from the user (into a local variable, not @name)
2.a) if it's "Quit", go to end
2.b) otherwise if it's not empty, assign it to @name
3. play​
--
Matthew Kerwin
http://matthew.kerwin.net.au/

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Thank you very much Matthew, i was wrong from the beginning :slight_smile:

So here it is :

--8<--

class Testing

   def initialize
     # Start with setting your name
     # Case 1 : name is empty => "Let's play Guest!"
     # Case 2 : name is a real name => "Let's play your_name"
     # Case 3 : name is 'quit' => "Goodbye Guest." & exit game

     @name = 'Guest'
     your_name
   end

   def your_name
     # Get your name
     puts "(Type 'quit' to exit)"
     print 'Please your name? '
     try_a_name = gets.chomp.capitalize

     @name = try_a_name unless try_a_name == 'Quit' || try_a_name.empty?
     byebye if try_a_name == 'Quit'

     play
   end

   def byebye
     puts "\nGoodbye #{@name}.\n\n"

     exit
   end

   def play
     puts "Let's play #{@name}!"
   end

end

Testing.new

-->8---

/Nathan

···

Le 2016-11-24 16:00, Matthew Kerwin a écrit :

On 24 November 2016 at 21:50, Nathan Guilty <ruby@e-solutions.re> > wrote:

This doesn't work :

1/ Set the name
Case 1 : If the name is empty so, i need to have "Let's play Guest!"
Case 2 : If the name is quit, i need to have "Goodbye Guest."
Case 3 : A real player name, i need to have "Let's play #{@name}"

:slight_smile:

​My brief sketch of an algorithm would be:

1. initialise the name to "Guest"

2. read a word from the user (into a local variable, not @name)

2.a) if it's "Quit", go to end

2.b) otherwise if it's not empty, assign it to @name

3. play​

--

Matthew Kerwin
http://matthew.kerwin.net.au/