Skip to content

Modified #to_s to format 'Q Hearts' to 'QH' #36

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

mjvezzani
Copy link

Finished Panda level requirements for Blackjack assignment. Based on what I observed from your programming style, I hacked together a working version first, and then figured out ways to slim down my code. At this point I'm not sure if it is a slim as it could be, but I'm relatively happy about it.

@@ -14,7 +14,12 @@ def value
end

def to_s
"#{@value}-#{suit}"
suit = "H" if @suit == :hearts
Copy link
Member

Choose a reason for hiding this comment

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

The way this is specified is very close to a case statement. In ruby, that looks like:

suit = case @suit
when :hearts
  "H"
when :clubs
  "C"
when :spades
  "S"
when :diamonds"
 "D"
end

However, there's a slight problem here, that if the suit is something other than a :hearts, :clubs, :spades:, :diamonds, it will be nil and cause problems later down the line. So you might:

suit = case @suit
when :hearts
  "H"
when :clubs
  "C"
when :spades
  "S"
when :diamonds
 "D"
else
  raise ArgumentError.new "Unsupported suit: #{suit}"
end

@jwo
Copy link
Member

jwo commented Nov 22, 2013

Thanks for the submission -- you have the procedure down!

I'd like you to think about this --

suit = "H" if @suit == :hearts

Is there another way to get "H" from :hearts ?

@mjvezzani
Copy link
Author

Doing a bit of research, I came up with this as a possible solution:

suit = case @suit
when :hearts
  :hearts.to_s.capitalize[0]
when :spades
  :spades.to_s.capitalize[0]
when :diamonds
  :diamonds.to_s.capitalize[0]
when :clubs
  :clubs.to_s.capitalize[0]
else
  raise ArgumentError.new "Unsupported suit: #{@suit}"
end

Then, to reduce verbosity, I could define a method #initial:

def initial(suit)
  suit.to_s.capitalize[0]
end

Which produces the following results:

initial(:spades) # => "S"
initial(:clubs) # => "C"
initial(:diamonds) # => "D"
initial(:hearts) # => "H"

Thus my final code would look like this:

suit = case @suit
when :hearts
  initial(:hearts)
when :diamonds
  initial(:diamonds)
when :spades
  initial(:spades)
when :clubs
  initial(:clubs)
else
  raise ArgumentError.new "Unsupported suit: #{@suit}"
end

…hod. Fixed my #to_s method with case statements and implemented my #initial(suit) method.
@jwo
Copy link
Member

jwo commented Nov 25, 2013

cool!

What about reducing the code down to

def to_s
  "#{@value}-#{suit.to_s.upcase[0]}"
end

or, to use the initial method:

def to_s
  "#{@value}-#{initial suit}"
end

def initial(suit)
  suit.to_s.capitalize[0]
end

Now THAT is some readable code :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants