Saturday, 24 August 2013

Why can't I override the * method in Ruby?

Why can't I override the * method in Ruby?

I'm working on a Ruby Gem for creating presentations, and I would like to
create a syntax for defining slides that is simple and intuitive. I'm
making use of instance_eval so I can call methods on self. Here's what I
originally planned to do:
slide {
title 'What is Ruby?'
* 'a programming language'
* 'with lots of interpreters'
* 'lots of fun!'
}
Even though I've defined a * method, I get the error:
in `instance_eval': ... syntax error, unexpected '\n', expecting :: or '['
or '.' (SyntaxError)
I have compromised by creating a short method called b for creating
bullets, but it's not as nice:
slide {
title 'What is Ruby?'
b 'a programming language'
b 'with lots of interpreters'
b 'lots of fun!'
}
Is this just a limitation of the interpreter? Or is there a way to get
around it?

No comments:

Post a Comment