Overriding String#[]

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.

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

end

def []=(key, value)

end

Thanks.

On 8/10/06, Lukasz M. [email protected] 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
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”

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

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"

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs