[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Overriding String#[]

Lukasz Muziol

8/11/2006 12:13:00 AM

The generic question would be "does anyone know a decent and complete
reference to Ruby's syntax?"

What interests me is the syntax required to define methods like [], []=,
getting their parameters in between the braces. It's fairly
inconvienient that String#[] returns a character code, rather than a
string of one character. Hope it'll get changed soon (to get in line
with POLS perhaps :)), accomodating my laziness for now I hope to
override it. How do I define a function named like this?

Thanks.

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

4 Answers

e

8/11/2006 12:22:00 AM

0

Lukasz Muziol wrote:
> The generic question would be "does anyone know a decent and complete
> reference to Ruby's syntax?"
>
> What interests me is the syntax required to define methods like [], []=,
> getting their parameters in between the braces. It's fairly
> inconvienient that String#[] returns a character code, rather than a
> string of one character. Hope it'll get changed soon (to get in line
> with POLS perhaps :)), accomodating my laziness for now I hope to
> override it. How do I define a function named like this?

def [](key)
# ...
end

def []=(key, value)
# ...
end

> Thanks.


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

Rick DeNatale

8/11/2006 12:29:00 AM

0

On 8/10/06, Lukasz Muziol <wookie2@o2.pl> wrote:
> The generic question would be "does anyone know a decent and complete
> reference to Ruby's syntax?"
>
> What interests me is the syntax required to define methods like [], []=,
> getting their parameters in between the braces. It's fairly
> inconvienient that String#[] returns a character code, rather than a
> string of one character. Hope it'll get changed soon (to get in line
> with POLS perhaps :)), accomodating my laziness for now I hope to
> override it. How do I define a function named like this?

Well the syntax is:

class String
def [](arg)
end
end

But, this one seems like a pretty dangerous mod to me, with the
potential to break lots of things.

But, keep in mind that while:

irb(main):001:0> "abc"[1]
=> 98

you can get the result you want without changing Strings [] method.

irb(main):002:0> "abc"[1..1]
=> "b"

--
Rick DeNatale
http://talklikeaduck.denh...

Matt Todd

8/11/2006 12:30:00 AM

0

Should be just like so:

def [] index
# handle index or regexp or whatever
end

I know that it seems odd, but you basically just put the skeleton of
the method name in there as the name and then the parameters are what
go into the expression. You could override the + method (and many
others) as well. For instance:

class Foo
def + operand
# do some merging or something of whatever Foo is
end
end

In that way, you can define some weird magic to happen, like taking
two ActiveRecord models in and spitting out a relationship, for
instance:

(Post.find_by_id(12) + Tag.find_or_create_by_tag('Ruby')).save

We call #save on what's returned because it's a PostTag model that's returned...

Of course, this could be ironed out a bit, but you see the point:
overload operators with just their names... as simple as

def + operand
# magic here
end

Etc, etc, etc.

M.T.

(Sorry, I'm a bit drowsy, so this might not make total sense.)

Mr Pinto

8/11/2006 3:48:00 AM

0

> But, keep in mind that while:
>
> irb(main):001:0> "abc"[1]
> => 98
>
> you can get the result you want without changing Strings [] method.
>
> irb(main):002:0> "abc"[1..1]
> => "b"

Or:
irb(main):001:0> str = "abc"[1].chr
=> "b"
irb(main):002:0> str.class
=> String

The []= method should already be doing what you want:

irb(main):001:0> str = "abc"
=> "abc"
irb(main):002:0> str[1] = "d"
=> "d"
irb(main):003:0> str
=> "adc"