• question regarding syntax of #{rand()}

    From Benjamin L. Russell@21:1/5 to All on Sat Apr 16 21:16:50 2022
    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^

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Martin Klaiber@21:1/5 to Benjamin L. Russell on Sat Apr 16 16:21:56 2022
    Benjamin L. Russell <DekuDekuplex@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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Benjamin L. Russell@21:1/5 to usenet.martinkl@gmx.de on Mon Apr 18 00:58:48 2022
    On Sat, 16 Apr 2022 16:21:56 +0200, Martin Klaiber
    <usenet.martinkl@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$B!G(Bt 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.
    --
    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^

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Sophia Ding@21:1/5 to Benjamin L. Russell on Tue May 10 09:44:20 2022
    On Sunday, April 17, 2022 at 10:58:58 AM UTC-5, Benjamin L. Russell wrote:
    On Sat, 16 Apr 2022 16:21:56 +0200, Martin Klaiber
    <usenet.martinkl@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.
    --
    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^
    IDK what who you are

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)