class Sword
def initialize(&block)
@damage = block
end
def realDam
@damage.call
end
end
Longsword = Sword.new { rand(8)+1 }
weapon = Longsword
5.times {p weapon.realDam}
puts
5.times {p(rand(8)+1)}
------- output --------
3
2
1
4
8
1
5
4
1
5
On 4/14/08, Ben Galyean <bengalyean / hotmail.com> wrote:
> Hello all,
>
> Just started my first Ruby prog and hit a snag almost instantly. I
> simply wish to have my selected weapon do it's prescribed damage as I
> intend (in this case, it's 1-8 damage). Complete noob here so any help
> at all would be greatly appreciated. The following is a re-creation of
> my problem:
>
> #***********************************
> class Sword
> attr_accessor(:realDam)
>
> def initialize(aRealDam)
> @realDam = aRealDam
> end
> end
>
> Longsword = Sword.new(rand(8)+1)
> weapon = Longsword
>
> begin
>
> 5.times {p weapon.realDam}
> puts
> 5.times {p (rand(8)+1)}
>
> end
> #*************************************
>
> Output looks like this:
>
> #****Current Longsword damage (Picks a number and 'keeps' it).****
> 6
> 6
> 6
> 6
> 6
>
> #****Intended output for the longsword here****
> 6
> 8
> 8
> 4
> 8
> --
> Posted via http://www.ruby-forum.com/.
>
>
--
Gerardo Santana