Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Overriding String#[]

Reply
Thread Tools

Overriding String#[]

 
 
Lukasz Muziol
Guest
Posts: n/a
 
      08-11-2006
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-forum.com/.

 
Reply With Quote
 
 
 
 
Eero Saynatkari
Guest
Posts: n/a
 
      08-11-2006
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-forum.com/.

 
Reply With Quote
 
 
 
 
Rick DeNatale
Guest
Posts: n/a
 
      08-11-2006
On 8/10/06, Lukasz Muziol <(E-Mail Removed)> 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.denhaven2.com/

 
Reply With Quote
 
Matt Todd
Guest
Posts: n/a
 
      08-11-2006
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.)

 
Reply With Quote
 
Mr Pinto
Guest
Posts: n/a
 
      08-11-2006
> 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"

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with overriding generic in top level fastgreen2000@yahoo.com VHDL 9 02-10-2005 05:54 PM
Redistribute static to OSPF, overriding the slower OSPF-native route? E.Finlayson Cisco 0 09-10-2004 02:13 PM
overriding,shadowing concept sangam via .NET 247 ASP .Net 1 06-23-2004 03:46 AM
A design ( inheriteance / overriding ) question sourabh ASP .Net 1 05-21-2004 06:25 AM
Overriding functionality of an entity is prohibited? Valentin Tihomirov VHDL 10 11-03-2003 07:41 PM



Advertisments