[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to write a destructive string method?

Henrik Nyh

1/6/2006 1:07:00 PM

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.

--
Posted via http://www.ruby-....


6 Answers

ts

1/6/2006 1:19:00 PM

0

>>>>> "H" == Henrik <henrik@nyh.se> writes:

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

replace(upcase)

H> end
H> end


Guy Decoux





Devin Mullins

1/6/2006 1:22:00 PM

0

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



Fritz Heinrichmeyer

1/6/2006 1:32:00 PM

0

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"





>
> 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.
>


--
Mit freundlichen Grü�en
Fritz Heinrichmeyer FernUniversität, LG ES, 58084 Hagen (Germany)
tel:+49 2331/987-1166 fax:987-355

Henrik Nyh

1/6/2006 1:35:00 PM

0

ts wrote:
>>>>>> "H" == Henrik <henrik@nyh.se> 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.

--
Posted via http://www.ruby-....


Florian Groß

1/6/2006 2:33:00 PM

0

dblack

1/6/2006 9:14:00 PM

0