Re: newbie Q - calling a method for an object in an .each bl



Logan Capaldo wrote:


It's working but #each returns the array and your #show method
returns a string, it doesn't have any side effects. You may want to
use map instead or use puts

e.g.:

hand.each { |c| puts c.show }

or hand.map { |c| c.show }

Ah! These work. Thank You!

I do recall from pickax that #each returns the array. It must be, then,
with

hand.each { |c| c.show }

what I am seeing how irb displays an array. Does this mean that the
string returned by #show is produced, but nothing is telling it to
display so it sort of ... gets assigned to nothing, and dosn't display
in irb?

But, then, why doesn't the block with the puts call first display the
strings followed by the display of the array returned by #each?

Follwing is the code defining the class (the rest I am just testing in
irb) for hal fulton, who asked for it. This is my first every ruby
program so it may be ugly, and could probably be improved upon. Been
programming in procedural languages since fortran 77, so this OO stuff
is a bit mind bending. But ... I like it!

class Card

attr_reader :suit, :value, :face

def initialize(suit,value)
raise(IndexError, "Card: Suite out of range: #{suit}") if suit !~
/[hcds]/
@suit = suit
raise(IndexError, "Card: Value out of range: #{value}" ) if not
((0..12) === value)
@value = value
@face = "Down"
end

def ssuit
case
when @suit == "h" then "Hearts"
when @suit == "c" then "Clubs"
when @suit == "d" then "Diamonds"
when @suit == "s" then "Spades"
end
end


def pips
case @value
when 0 .. 8 then " " + (@value+2).to_s + " "
when 9 then " J "
when 10 then " Q "
when 11 then " K "
when 12 then " A "
end
end


def show
if @face == "Down"
"Card: xxx xx xxxxx"
else
"Card: #{pips} of #{ssuit}"
end
end

def flip
if @face == "Down"
then @face = "Up"
else @face = "Down"
end
end

end


--
Posted via http://www.ruby-forum.com/.

.



Relevant Pages

  • [SUMMARY] DayRange (#92)
    ... A couple of submitters mentioned that this problem isn't quite as simple as it ... def test_english ... This is the heart of the String building process and many solutions landed on ... Array of Arrays with the days divided into groups of start and end days. ...
    (comp.lang.ruby)
  • Re: Speed issues iterating over chars
    ... store scores as an Array of Fixnum instead of using #ord. ... Then we plug this onto the sequence string: ... Score = Struct.new:seq,:score ... def mask_sequence ...
    (comp.lang.ruby)
  • Re: Lots of Response.Writes of HTML - How do YOU do it?
    ... Dimension array ... // and creates a long html string accordingly to display the info. ...
    (microsoft.public.inetserver.asp.general)
  • Re: Attaching files in windows using Python.
    ... import win32gui, struct, array, string ... def arrayToStrings: ... """build an OPENFILENAME struct as a string, ...
    (comp.lang.python)
  • Re: vb.net search network drive
    ... files to display what I have above. ... Dim sFilesAs String ... array of strings - so what we are doing is matching up the sFiles ... UBound comes into play. ...
    (microsoft.public.dotnet.languages.vb)