Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #5053

Re: Method that mutates object

From Quintus <sutniuq@gmx.net>
Newsgroups comp.lang.ruby
Subject Re: Method that mutates object
Date 2011-05-25 15:45 -0500
Organization Service de news de lacave.net
Message-ID <4DDD6A71.7090502@gmx.net> (permalink)
References <9004f010954dd5cee76c02c11f61ad58@ruby-forum.com>

Show all headers | View raw


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Am 25.05.2011 21:22, schrieb jason solomon:
> Say we want to write a String method called clear that takes a given
> string and modifies that string to be equal to "".
> 
> Ex.
> 
> str = "string"
> 
> we call str.clear and we get back "", not just "" printed to the screen,
> but the value of str is now "" (mutate the original string).
> 
> 
> I understand that the following code would just print "" to the screen
> but not modify the actual str object.
> 
> class String
>   def clear
>     ""
>   end
> end
> 
> 
> How would you write a method that actually modifies the str object?
> 

Just in addition to what the others said, the method String#clear
already exists.

=================================================
$ ri String#clear
= String#clear

(from ruby core)
-
------------------------------------------------------------------------------
  string.clear    ->  string

-
------------------------------------------------------------------------------

Makes string empty.

       a = "abcde"
       a.clear    #=> ""
=================================================

And does exactly what you want it to.

Vale,
Marvin
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJN3WptAAoJELh1XLHFkqhaG5AH/1mAcLBo9eKzpJg3XHjIXXAr
vFOUV9+Ow3hSBZ8Q1KDQrKOVd2QBRkkA2vk7wF7H9KrJCar7wElf0OgJHYmjlEhR
HRnnxtoRgdkRo1GoFyvGr7tRzoKlAgPNC93AQFDm7mZFfO6QMtF5aR4xLgWMIZhM
vRNMkjs71XE1lmxASvFDVgW4SS1wxJnzDe0LQZncjOOPaZHQQM2P3aR9mHL8Or8O
FBvuzqgCptKq5Y69xv/AysLZlRu1ja37J6ggK7LIuLsmT2UaydOc9pVgDaunazrI
ym9IASTm/7vmYRKr3FuA98JifEGAEWC6jkuP0MYcLNkGLdhEzkce04FxHdkfodk=
=5xGw
-----END PGP SIGNATURE-----

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Method that mutates object jason solomon <solomon.jas@gmail.com> - 2011-05-25 14:22 -0500
  Re: Method that mutates object Stefano Crocco <stefano.crocco@alice.it> - 2011-05-25 14:28 -0500
    Re: Method that mutates object jason solomon <solomon.jas@gmail.com> - 2011-05-25 15:52 -0500
  Re: Method that mutates object Adam Prescott <adam@aprescott.com> - 2011-05-25 14:31 -0500
  Re: Method that mutates object Quintus <sutniuq@gmx.net> - 2011-05-25 15:45 -0500
    Re: Method that mutates object jason solomon <solomon.jas@gmail.com> - 2011-05-25 16:03 -0500
  Re: Method that mutates object jason solomon <solomon.jas@gmail.com> - 2011-05-25 15:59 -0500
    Re: Method that mutates object Brian Candler <b.candler@pobox.com> - 2011-05-25 16:20 -0500
  Re: Method that mutates object "jay s." <solomon.jas@gmail.com> - 2011-05-25 16:36 -0500
    Re: Method that mutates object 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-25 17:00 -0500
  Re: Method that mutates object "jay s." <solomon.jas@gmail.com> - 2011-05-26 08:56 -0500
    Re: Method that mutates object Jeremy Bopp <jeremy@bopp.net> - 2011-05-26 09:43 -0500
      Re: Method that mutates object "jay s." <solomon.jas@gmail.com> - 2011-05-26 10:00 -0500
        Re: Method that mutates object Josh Cheek <josh.cheek@gmail.com> - 2011-05-26 13:12 -0500
  Re: Method that mutates object "jay s." <solomon.jas@gmail.com> - 2011-05-26 10:55 -0500
  Re: Method that mutates object "jay s." <solomon.jas@gmail.com> - 2011-05-26 11:07 -0500
    Re: Method that mutates object Jeremy Bopp <jeremy@bopp.net> - 2011-05-26 11:14 -0500
      Re: Method that mutates object "jay s." <solomon.jas@gmail.com> - 2011-05-26 11:30 -0500
    Re: Method that mutates object Gary Wright <gwtmp01@mac.com> - 2011-05-26 16:34 -0500
      Re: Method that mutates object Adam Prescott <adam@aprescott.com> - 2011-05-27 10:57 -0500
      Re: Method that mutates object Christopher Dicely <cmdicely@gmail.com> - 2011-05-28 13:05 -0500
        Re: Method that mutates object Gary Wright <gwtmp01@mac.com> - 2011-05-28 19:29 -0500
          Re: Method that mutates object Gary Wright <gwtmp01@mac.com> - 2011-05-28 21:06 -0500

csiph-web