Re: newbie Q - calling a method for an object in an .each bl
- From: william robb <otherwill@xxxxxxxxx>
- Date: Sat, 8 Jul 2006 14:40:37 +0900
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/.
.
- Follow-Ups:
- Re: newbie Q - calling a method for an object in an .each bl
- From: Robert Klemme
- Re: newbie Q - calling a method for an object in an .each bl
- References:
- newbie Q - calling a method for an object in an .each block?
- From: william robb
- Re: newbie Q - calling a method for an object in an .each block?
- From: Logan Capaldo
- newbie Q - calling a method for an object in an .each block?
- Prev by Date: Re: Enumerable#split, any advice on this implementation ?
- Next by Date: Re: ASN.1 for Ruby
- Previous by thread: Re: newbie Q - calling a method for an object in an .each block?
- Next by thread: Re: newbie Q - calling a method for an object in an .each bl
- Index(es):
Relevant Pages
|