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