Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

question regarding syntax of #{rand()}

27 views
Skip to first unread message

Benjamin L. Russell

unread,
Apr 16, 2022, 8:17:02 AM4/16/22
to
On page 194 of the book _Ruby Wizardry_, by Eric Weinstein, the
following function appears:

def attack
puts "Did #{rand(strength)} damage!"
end

According to the text, this function should exhibit the following
behavior in irb:

>> rex = GuardDog.new('Rex', 7)
=> #<GuardDog:0x0000010334e168 @strength=7, @name="Rex">
>> rex.attack
Did 1 damage!
=> nil
>> rex.attack
Did 4 damage!
=> nil

However, instead, rex.attack exhibits the following behavior:

>> irb(main):006:0> rex.attack
>> Did #{@damage} damage!
>> => nil

The complete code for my guard_dog.rb file, which contains the
relevant functions, is as follows:

class Animal
attr_accessor :name

def initialize(name, legs=4)
@name = name
@legs = legs
end
end

class Dog < Animal
attr_accessor :name

def initialize(name)
@name = name
end

def bark
puts 'Arf!'
end
end

class GuardDog < Dog
attr_accessor :strength

def initialize(name, strength)
@strength = strength
super(name)
end

def bark
puts 'Stop, in the name of the law!'
end

def attack
@damage = rand(@strength)
puts 'Did #{@damage} damage!'
end
end

Why doesn't rex.attack exhibit the desired behavior?
--
Benjamin L. Russell / DekuDekuplex at Yahoo dot com
http://dekudekuplex.wordpress.com/
Computer Science Document Proofreader/Editor
"Furuike ya, kawazu tobikomu mizu no oto." -- Matsuo Basho^

Martin Klaiber

unread,
Apr 16, 2022, 10:38:15 AM4/16/22
to
Benjamin L. Russell <DekuDe...@yahoo.com> wrote:

> def attack
> @damage = rand(@strength)
> puts 'Did #{@damage} damage!'
> end
> end

> Why doesn't rex.attack exhibit the desired behavior?

Here it works when using double-quotes instead of single:

| puts "Did #{@damage} damage!"

Proof:

| martinkl@maurice:~/src/eigene/ruby$ ./guard_dog.rb
| Did 0 damage!
| martinkl@maurice:~/src/eigene/ruby$ ./guard_dog.rb
| Did 6 damage!
| martinkl@maurice:~/src/eigene/ruby$ ./guard_dog.rb
| Did 2 damage!
| martinkl@maurice:~/src/eigene/ruby$

HTH, Martin

Benjamin L. Russell

unread,
Apr 17, 2022, 11:58:58 AM4/17/22
to
On Sat, 16 Apr 2022 16:21:56 +0200, Martin Klaiber
<usenet....@gmx.de> wrote:

>Here it works when using double-quotes instead of single:
>
>| puts "Did #{@damage} damage!"
>
>Proof:
>
>| martinkl@maurice:~/src/eigene/ruby$ ./guard_dog.rb
>| Did 0 damage!
>| martinkl@maurice:~/src/eigene/ruby$ ./guard_dog.rb
>| Did 6 damage!
>| martinkl@maurice:~/src/eigene/ruby$ ./guard_dog.rb
>| Did 2 damage!
>| martinkl@maurice:~/src/eigene/ruby$

Ah, I see; according to p. 35 of the book,

> Well, the #{} magic (called string interpolation if you
> want to be super fancy) is possible only with
> double-quoted strings; it can’t be done with
> single-quoted ones.

Apparently, I had better either differentiate more closely between
single and double quotes when reading the book, or increase my intake
of ginkgo biloba (which I have been too stingy to take recently) to
increase my memory.

Thank you for your response; issue resolved.

Sophia Ding

unread,
May 10, 2022, 12:44:24 PM5/10/22
to
IDK what who you are
0 new messages