Modified #to_s to format 'Q Hearts' to 'QH'#36
Modified #to_s to format 'Q Hearts' to 'QH'#36mjvezzani wants to merge 3 commits intoRubyoffRails:masterfrom
Conversation
blackjack.rb
Outdated
There was a problem hiding this comment.
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"
endHowever, 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|
Thanks for the submission -- you have the procedure down! I'd like you to think about this -- suit = "H" if @suit == :heartsIs there another way to get "H" from |
|
Doing a bit of research, I came up with this as a possible solution: Then, to reduce verbosity, I could define a method #initial: Which produces the following results: Thus my final code would look like this: |
…hod. Fixed my #to_s method with case statements and implemented my #initial(suit) method.
|
cool! What about reducing the code down to def to_s
"#{@value}-#{suit.to_s.upcase[0]}"
endor, to use the initial method: def to_s
"#{@value}-#{initial suit}"
end
def initial(suit)
suit.to_s.capitalize[0]
endNow THAT is some readable code :) |
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.