SwiftMango
SwiftMango

Reputation: 15294

How to create a percent method in Ruby like %w, or %x?

I tried to make a method (or notation?) like %w, but ruby is not happy with the name of method and give errors all the time. I tried to search a bit, but did not find anything exciting.

Any ideas how to do it?

For example,

def %f(&block)
    puts "foo"
end

Upvotes: 2

Views: 224

Answers (3)

Andrew Grimm
Andrew Grimm

Reputation: 81530

You'd have to edit Ruby itself in order to do that, at least for YARV Ruby, the C-based implementation of Ruby 1.9 .

parse.y famtour slides 63 to 76 gives an example of someone editing Ruby itself to create another percent method. Not very practical for work situations!

Upvotes: 2

Jörg W Mittag
Jörg W Mittag

Reputation: 369468

Those aren't methods, those are literal syntax. There are very few languages that do allow creating new literals, but Ruby isn't one of them. Like most languages, the syntax is fixed.

Upvotes: 3

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230366

You can't do that. These shortcuts are part of the language.

Upvotes: 4

Related Questions