Forum: Ruby Writing method "change!" for String

637f745e44c81f4830ba7dca35c800b4?d=identicon&s=25 Mike (Guest)
on 2007-06-05 14:20
(Received via mailing list)
Hi,

   I'm trying to write simple method to change internal value of
String class:

class String
    def change!
        self="0" if self.downcase=="myvalue1"
        self="1" if self.downcase=="myvalue2"
        self
    end
end

test="myvalue1"
test.change!
p test

It should just change value of string to what I want. But I get error:
"Can't change the value of self".
How do I proceed?

Thanks.
97550977337c9f0a0e1a9553e55bfaa0?d=identicon&s=25 Jano Svitok (Guest)
on 2007-06-05 14:29
(Received via mailing list)
On 6/5/07, Mike <michaelst@gmail.com> wrote:
>     end
> Thanks.
use String#replace instead of assignment. Beware of the implications!
(i.e. you are changing the string the variable points to!) for
example:

...

test = "myvalue1"
foo = test
bar = test

test.change!

p foo #=> "0"
p bar #-=> "0"

If you don't want this behaviour return new value instead of replace
and do an assigment to variable:

test = test.change
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2007-06-05 14:31
(Received via mailing list)
On 6/5/07, Mike <michaelst@gmail.com> wrote:
> Hi,
>
>    I'm trying to write simple method to change internal value of
> String class:
>
> class String
>     def change!
>         self="0" if self.downcase=="myvalue1"

>         self="1" if self.downcase=="myvalue2"
>         self
>     end
> end
>
class String
   def change!
       case downcase
          when "myvalue"
            replace("0")
         etc. etc.
> test="myvalue1"
> test.change!
> p test
>
> It should just change value of string to what I want. But I get error:
> "Can't change the value of self".
> How do I proceed?
>

HTH
Robert
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-06-05 14:33
(Received via mailing list)
On 05.06.2007 14:16, Mike wrote:
>     end
> end
>
> test="myvalue1"
> test.change!
> p test
>
> It should just change value of string to what I want. But I get error:
> "Can't change the value of self".
> How do I proceed?

You don't.  Just use String#replace.

Btw, what are you trying to achieve?

Kind regards

  robert
637f745e44c81f4830ba7dca35c800b4?d=identicon&s=25 Mike (Guest)
on 2007-06-05 14:35
(Received via mailing list)
On Jun 5, 10:26 pm, Robert Klemme <shortcut...@googlemail.com> wrote:
> >     def change!
> > It should just change value of string to what I want. But I get error:
> > "Can't change the value of self".
> > How do I proceed?
>
> You don't.  Just use String#replace.
>
> Btw, what are you trying to achieve?
>
> Kind regards
>
>         robert

I missed that method. Thank you.
918c6daad03c85e51ad1a11f57017947?d=identicon&s=25 Devin Mullins (twifkak)
on 2007-06-05 14:37
(Received via mailing list)
Mike wrote:
> It should just change value of string to what I want. But I get error:
> "Can't change the value of self".

You really can't. Consider:
class Fixnum
   def double!
     self *= 2
   end
end
3.double! # wha happen?

> How do I proceed?
ri String#replace
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-06-05 14:38
(Received via mailing list)
Hi --

On Tue, 5 Jun 2007, Mike wrote:

>    end
> end
>
> test="myvalue1"
> test.change!
> p test
>
> It should just change value of string to what I want. But I get error:
> "Can't change the value of self".
> How do I proceed?

ri String#replace


David
2f4d4f9c35ea851bffb9a9cc2e086365?d=identicon&s=25 Harry Kakueki (Guest)
on 2007-06-05 14:38
(Received via mailing list)
On 6/5/07, Mike <michaelst@gmail.com> wrote:
>     end
> Thanks.
>
>
>
Do not try to change self.

Try this.

class String
   def change!
       val="0" if self.downcase=="myvalue1"
       val="1" if self.downcase=="myvalue2"
       val
   end
end

test="myvalue1"
p test.change!
p test

Harry

--

A Look into Japanese Ruby List in English
http://www.kakueki.com/
C06869c119472a139eb163b72040b0db?d=identicon&s=25 Bertram Scharpf (Guest)
on 2007-06-05 14:40
(Received via mailing list)
Hi,

Am Dienstag, 05. Jun 2007, 21:30:13 +0900 schrieb Robert Dober:
> class String
>   def change!
>       case downcase
>          when "myvalue"
>            replace("0")
>         etc. etc.

alternatively:

  class String
    def change!
      case self
         when /myvalue1/i then replace "0"
         when /myvalue2/i then replace "1"
        etc. etc.


Bertram
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-06-05 15:17
(Received via mailing list)
Hi --

On Tue, 5 Jun 2007, Harry Kakueki wrote:

>>         self
>>
>      val="0" if self.downcase=="myvalue1"
>      val="1" if self.downcase=="myvalue2"
>      val
>  end
> end
>
> test="myvalue1"
> p test.change!
> p test

I think even the original idea probably wasn't a good !-candidate
(since bang methods generally have non-bang equivalents, whereas
methods with names that imply change or other "danger" [like replace]
don't), and definitely if you're not modifying the string or doing
anything else dangerous, the ! is not a good idea, since it's out of
keeping with the convention.


David
C06869c119472a139eb163b72040b0db?d=identicon&s=25 Bertram Scharpf (Guest)
on 2007-06-05 15:51
(Received via mailing list)
Hi,

Am Dienstag, 05. Jun 2007, 22:16:33 +0900 schrieb dblack@wobblini.net:
>
> I think even the original idea probably wasn't a good !-candidate
> (since bang methods generally have non-bang equivalents, whereas
> methods with names that imply change or other "danger" [like replace]
> don't),

As far as I see banged methods are mostly implemented as

  class String
    def modify_it!
      ...
    end
    def modify_it
      str = dup
      str.modify_it!
      str
    end
  end

(Of course some C equivalent.)

So, if Mike means what he does he can easily add the
counterpart.

Bertram
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.