From: Иван Сташко on
I would like to create a new general delimiter.

---------- Background

Suppose you have created a specific data type which is a combination of
various other native data types, such as strings and numbers.

Suppose that these are needed throughout your code, so you would like to
quickly express them.

I have found one way to do this,

class WhatEver
.
.
.
end

def we(o)
WhatEver.new(o)
end

so that I can type

a = we " This 73 brqx "

in my code as a shorthand for

a = WhatEver.new "This 73 brqx"

--------- Question

I think code would be more readable if I could type,

a = %e "This 73 brqx"

-> I know there is metaprogramming in Ruby, and Ruby is quite flexible
-> The question is... Is this possible? Where is %q, for example,
defined? It does not appear to be a method of String or Object or
Kernel
-> OK, so let's say it's something in C. Where would I modify the C to
get this if I would want to?
--
Posted via http://www.ruby-forum.com/.

From: Marvin Gülker on
Иван Сташко wrote:
> I would like to create a new general delimiter.
>
> ---------- Background
>
> Suppose you have created a specific data type which is a combination of
> various other native data types, such as strings and numbers.
>
> Suppose that these are needed throughout your code, so you would like to
> quickly express them.
>
> I have found one way to do this,
>
> class WhatEver
> .
> .
> .
> end
>
> def we(o)
> WhatEver.new(o)
> end
>
> so that I can type
>
> a = we " This 73 brqx "
>
> in my code as a shorthand for
>
> a = WhatEver.new "This 73 brqx"
>
> --------- Question
>
> I think code would be more readable if I could type,
>
> a = %e "This 73 brqx"
>
> -> I know there is metaprogramming in Ruby, and Ruby is quite flexible
> -> The question is... Is this possible? Where is %q, for example,
> defined? It does not appear to be a method of String or Object or
> Kernel
> -> OK, so let's say it's something in C. Where would I modify the C to
> get this if I would want to?

%q is part of Ruby's syntax and not a method. You can't modify how Ruby
code is parsed via metaprogramming, the only way possible is to modify
Ruby's C sources before you compile Ruby.

What is possible is to "absuse" the special ` method (you know, it's
usually for executing system commands). Although `cmd` looks like a
literal, it's actually the method Kernel#` and as such it can be
overriden:

irb(main):001:0> def `(str)
irb(main):002:1> puts str
irb(main):003:1> end
=> nil
irb(main):004:0> `abc`
abc
=> nil
irb(main):005:0>

Note that it's possible to do limit the redefined ` to a namespace:

irb(main):001:0> class Foo
irb(main):002:1> def `(str)
irb(main):003:2> puts "Got #{str}."
irb(main):004:2> end
irb(main):005:1> def bar
irb(main):006:2> `echo hello`
irb(main):007:2> end
irb(main):008:1> end
=> nil
irb(main):009:0> `echo hello`
=> "hello\n"
irb(main):010:0> Foo.new.bar
Got echo hello.
=> nil
irb(main):011:0> module Bar
irb(main):012:1> class << self
irb(main):013:2> def `(str)
irb(main):014:3> puts "Got #{str}."
irb(main):015:3> end
irb(main):016:2> end
irb(main):017:1> `echo hello`
irb(main):018:1> end
Got echo hello.
=> nil
irb(main):019:0> `echo hello`
=> "hello\n"
irb(main):020:0>

I've never seen this anywhere, so I suppose it's considered bad style.

Marvin
--
Posted via http://www.ruby-forum.com/.

From: Marvin Gülker on
Marvin Gülker wrote:
> What is possible is to "absuse" the special ` method

I meant "abuse", sorry.

Marvin
--
Posted via http://www.ruby-forum.com/.

From: Иван Сташко on
Thank you for the answer. So as I understand it, to get the effect that
I want, I would have to do some C coding. This might be interesting, I
have to think about it.

Overlaying ` like you describe is also interesting but seems useful to
protect unsafe code from executing unsafe commands... Other than that,
a single tick will not stand out in the code clearly to indicate what I
want.

Well, at least I know the answer now. I will just put this away for a
while. Thank you.
--
Posted via http://www.ruby-forum.com/.

From: Caleb Clausen on
On 7/25/10, Иван Сташко <johns(a)appliedfilter.com> wrote:
> --------- Question
>
> I think code would be more readable if I could type,
>
> a = %e "This 73 brqx"

I have in the past created preprocessors to achieve this type of
effect, based on my library RubyLexer. RubyLexer turns ruby source
into a stream of tokens, which can then be converted back to the same
source code. It can fairly easily be extended to recognize new token
types. And it's all written in ruby, so understanding and modifying
the MRI lexer c code is not necessary. I believe this would be much
easier to do than hacking on MRI c code, but it is nowhere near as
easy as redefining operators is. There is some knowledge of RubyLexer
internals required, tho, and I can help you with this if you'd like to
try.

 | 
Pages: 1
Prev: nmap-1.1.0
Next: * operator, Float