Overriding String#[]

L

Lukasz Muziol

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

Eero Saynatkari

Lukasz said:
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
 
R

Rick DeNatale

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"
 
M

Matt Todd

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

Mr Pinto

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"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top