Forum: Ruby How to write a destructive string method?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Henrik (Guest)
on 2006-01-06 15:07
I haven't been able to figure out how to write a destructive string
method of my own. How do I change the value of "self"? This is what I
tried first:

class String
	alias_method(:orig_upcase, :upcase)

	def upcase
	  orig_upcase.tr("åäö", "���")
	end

	def upcase!
	  self = self.upcase
	end
end

"upcase" works fine but "upcase!" doesn't - "can't change the value of
self". Nor can I do (assuming another alias_method):

	def upcase!
	  self.old_upcase!
	  self.tr!("åäö", "���")
	end

or the same thing chained (self.old_upcase!.tr!(...)), because that only
returns nil. I've even grasped for straws like

	def upcase!
	  =(self.upcase)
	end

with no luck. Very grateful for any help.
ts (Guest)
on 2006-01-06 15:20
(Received via mailing list)
>>>>> "H" == Henrik  <removed_email_address@domain.invalid> writes:

H> 	def upcase!
H> 	  self = self.upcase

          replace(upcase)

H> 	end
H> end


Guy Decoux
Devin M. (Guest)
on 2006-01-06 15:23
(Received via mailing list)
Henrik wrote:

>I haven't been able to figure out how to write a destructive string
>method of my own. How do I change the value of "self"?
>
ri String#replace

I don't know if that'll help, 'cause I don't know why this didn't work:

>	def upcase!
>	  self.old_upcase!
>	  self.tr!("åäö", "���")
>	end
>
>
Devin
Henrik (Guest)
on 2006-01-06 15:34
ts wrote:
>>>>>> "H" == Henrik  <removed_email_address@domain.invalid> writes:
>
> H> 	def upcase!
> H> 	  self = self.upcase
>
>           replace(upcase)
>
> H> 	end
> H> end
>
>
> Guy Decoux

Worked fine, thanks! Got some help on #ruby-lang, apparently you can do
stuff like the above and

self[0..-1] = "whatever"
but not
self = "whatever"

seeing as self is an object, not a variable - if anyone else was
wondering.
Fritz Heinrichmeyer (Guest)
on 2006-01-06 15:50
(Received via mailing list)
Henrik schrieb:
> I haven't been able to figure out how to write a destructive string
> method of my own. How do I change the value of "self"? This is what I
> tried first:

it makes no sense to change self, but you can change the data of self:

---- test.rb
class String
   def makefirstA
     self[0]= 65
     self
   end
end

x="xxxx"
p x.makefirstA
p x
--- end of test.rb

delivers

ruby test.rb
"Axxx"
"Axxx"
Florian GroÃ? (Guest)
on 2006-01-06 16:32
(Received via mailing list)
Henrik wrote:

> Nor can I do (assuming another alias_method):
>
> 	def upcase!
> 	  self.old_upcase!
> 	  self.tr!("åäö", "���")
> 	end
>
> or the same thing chained (self.old_upcase!.tr!(...)), because that only
> returns nil.

I'd do it like this: (Ignoring the more obvious replace solution)

class String
   alias :old_upcase! :upcase!
   def upcase!()
     old_upcase!
     tr!("åäö", "���")
   end

   def upcase()
     result = self.clone
     result.upcase!
     return result
   end
end
unknown (Guest)
on 2006-01-06 23:16
(Received via mailing list)
On Fri, 6 Jan 2006, ts wrote:

>>>>>> "H" == Henrik  <removed_email_address@domain.invalid> writes:
>
> H> 	def upcase!
> H> 	  self = self.upcase
>
>          replace(upcase)

   "self = self.upcase".replace("replace(upcase)")

(Sorry, couldn't resist.)


David
--
David A. Black
removed_email_address@domain.invalid

"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.manning.com/books/black
This topic is locked and can not be replied to.