[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Need - macros feature

Henry Savr

8/17/2006 2:19:00 PM

Dear Ruby gurus:

Are there Ruby macro features as a part of Ruby standard?
I want them to work with ANY editor or interactive environment and with
ANY OS

Why?

Because it is distracting to key-in:

def a1
@a1
end
def a1=(value)
@a1=value
end

when you are thinking just about class' very general behaviour.
I would prefer to key-in something like this initially:

%def a1

and POSSIBLY add: -r, or -w, or -rw
on early design stage. Entering all the juzz above distracts strongly.
I'm going to work on details later.

Besides, I work on various systems and I want to have unified macro
options just by installing Ruby.

Thank you,
Henry

--
Posted via http://www.ruby-....

14 Answers

Farrel Lifson

8/17/2006 2:24:00 PM

0

On 17/08/06, Henry Savr <hsavr@yahoo.com> wrote:
> Dear Ruby gurus:
>
> Are there Ruby macro features as a part of Ruby standard?
> I want them to work with ANY editor or interactive environment and with
> ANY OS
>
> Why?
>
> Because it is distracting to key-in:
>
> def a1
> @a1
> end
> def a1=(value)
> @a1=value
> end
>
> when you are thinking just about class' very general behaviour.
> I would prefer to key-in something like this initially:
>
> %def a1
>
> and POSSIBLY add: -r, or -w, or -rw
> on early design stage. Entering all the juzz above distracts strongly.
> I'm going to work on details later.
>
> Besides, I work on various systems and I want to have unified macro
> options just by installing Ruby.
>
> Thank you,
> Henry
>
> --
> Posted via http://www.ruby-....
>
>

attr_accessor :a1 # RW
attr_reader :a1 # R
attr_writer :a1 # W

Farrel

Henry Savr

8/17/2006 2:47:00 PM

0

Farrel Lifson wrote:
> On 17/08/06, Henry Savr <hsavr@yahoo.com> wrote:
>> def a1
>>
>> --
>> Posted via http://www.ruby-....
>>
>>
>
> attr_accessor :a1 # RW
> attr_reader :a1 # R
> attr_writer :a1 # W
>
> Farrel
Thank you,
but my question was about MACRO option, so the preprocessor should work
and produce ruby source


--
Posted via http://www.ruby-....

Pierre Barbier de Reuille

8/17/2006 2:58:00 PM

0

Henry Savr wrote:
> Farrel Lifson wrote:
>
>> On 17/08/06, Henry Savr <hsavr@yahoo.com> wrote:
>>
>>> def a1
>>>
>>> --
>>> Posted via http://www.ruby-....
>>>
>>>
>>>
>> attr_accessor :a1 # RW
>> attr_reader :a1 # R
>> attr_writer :a1 # W
>>
>> Farrel
>>
> Thank you,
> but my question was about MACRO option, so the preprocessor should work
> and produce ruby source
>
The good question is: what for ? In the case of the accessors, the
methods are define when the class is evaluated ... but when it is used,
there is no difference between accessors defined that way and accessors
defined the Hard Way (tm). The Good Thing (tm) with a dynamic language
as Ruby is you can safely generate your code (or parametrize it) so as
to avoid lot of typing, without any need for another macro-language, so
why not using this capability ?

Pierre

Austin Ziegler

8/17/2006 3:50:00 PM

0

On 8/17/06, Henry Savr <hsavr@yahoo.com> wrote:
> Thank you,
> but my question was about MACRO option, so the preprocessor should work
> and produce ruby source

There is no preprocessor for Ruby.

Farrel's suggestion (metaprogramming with attr_accessor) is the right one.

-austin
--
Austin Ziegler * halostatue@gmail.com * http://www.halo...
* austin@halostatue.ca * http://www.halo...feed/
* austin@zieglers.ca

Erik Veenstra

8/17/2006 5:13:00 PM

0

> There is no preprocessor for Ruby.
>
> Farrel's suggestion (metaprogramming with attr_accessor) is
> the right one.

Yep, you're right. But he wants to know how to do it himself.
He wants to understand Why [1]... ;]

gegroet,
Erik V. - http://www.erikve...

[1] http://whytheluckystiff.net/articles/seeingMetaclassesCl...

PS: Beer, downtown Amsterdam, tonight. No pancakes.

----------------------------------------------------------------

## Abstraction through meta-programming

## Getters and Setters - A Demo

# Consider this code:

class Foo
def bar
@bar
end

def bar=(new_bar)
@bar = new_bar
end
end

# This can be rewritten to this:

class Foo
def self.getter(iv)
define_method("#{iv}") do
instance_variable_get("@#{iv}")
end
end

def self.setter(iv)
define_method("#{iv}=") do |new_value|
instance_variable_set("@#{iv}", new_value)
end
end

getter :bar
setter :bar
end

# Which can be rewritten to this:

class Module
def getter(iv)
define_method("#{iv}") do
instance_variable_get("@#{iv}")
end
end

def setter(iv)
define_method("#{iv}=") do |new_value|
instance_variable_set("@#{iv}", new_value)
end
end
end

class Foo
getter :bar
setter :bar
end

# Which can be rewritten to this:

require "getter_and_setter.rb" # Fill it your self... ;]

class Foo
getter :bar
setter :bar
end

# Use it:

foo = Foo.new
foo.bar = "Hello World!"
puts foo.bar

----------------------------------------------------------------


John Carter

8/18/2006 4:28:00 AM

0

Henry Savr

8/24/2006 4:50:00 PM

0

Thank you, guys
Although it's not what I wanted, I have a lot of ideas in some
directions. It's good for new in Ruby.
Henry

--
Posted via http://www.ruby-....

Matthew Johnson

8/24/2006 4:52:00 PM

0

> Thank you, guys
> Although it's not what I wanted, I have a lot of ideas in some
> directions. It's good for new in Ruby.
> Henry

Can you describe some of your ideas? That would certainly help
foster discussion...

Matthew


Henry Savr

8/24/2006 5:00:00 PM

0

Matthew Johnson wrote:
>> Thank you, guys
>> Although it's not what I wanted, I have a lot of ideas in some
>> directions. It's good for new in Ruby.
>> Henry
>
> Can you describe some of your ideas? That would certainly help
> foster discussion...
>
> Matthew

Sure I will, but I am very short in time now. That was why I could not
be here earlier. Next week is for sure.

--
Posted via http://www.ruby-....

Henry Savr

8/28/2006 5:12:00 PM

0

Matthew Johnson wrote:
>> Thank you, guys
>> Although it's not what I wanted, I have a lot of ideas in some
>> directions. It's good for new in Ruby.
>> Henry
>
> Can you describe some of your ideas? That would certainly help
> foster discussion...
>
> Matthew

There is nothiing new. It's as old as Univac
What I want is a classic Macroprocessor tool.
I mean:
I keep pieces of souce code in some place.
They may be included in source code as is. More interesting, you can
adjust macroprocessor's output (hence, the input for Ruby interpeter)
using paremters.

E X A M P L E:
===============
You wrote the macro once and put it in macro library
(here I "invented" the "macrolanguage" on-fly, while writing the
example. I hope, the "language" is clear)

====this is a macro %var with argument %%x definition==============
macro: %var (%%x)

{def %%x=(value)
@%%x = value
return @%%x
end}
======== end of macro %var definition =============================
You may consider the macro definition as a template.


Now, somewhere in your code you write:
...
%var(my_lovely_var)
...

Once found the %var, macroprocessor should make substitutions and
produce this piece:

def my_lovely_var=(value)
@my_lovely_var = value
return @my_lovely_var
end

which will be inserted into code on the place, where the statement
%var(my_lovely_var) was instead of it.



Ruby style allows to expect, that it may be done on-fly.

So, finally the Ruby interpreter will execute the macroprocessor's
output, not input. Besides the macroprocessor's input and output are
saved, so you can return to them and change the simple lines of
get/set-code to something more complicated and valueable.

Again the get/set operations are just an EXAMPLE, used because everyone
is familiar with this piece of code. Sure, using attributes is kind of
solution for this particular case. (BTW. Thank you, Farrel Lifson, you
post convinced me to learn more about attributes on this very beginning
stage, and that was very usefull). I want the macro power options
themselves.

Sure, every language may consider any code as an input string, and
process the string, (and Ruby itself is a great for these purposes), but
macroprocessor is something very specialized for swiftness and
convinience, to do particular job of entering the code. Ruby's
macroprocessor tool may be specialized for entering Ruby code, which
would make it more powerful.

There are plenty of editors with comprehensive macro functions. But they
are OS dependent. As Ruby is claimed to be system independent, I would
prefer to have Ruby's own macroprocessor, built for Ruby in mind.

More I am writing on this topic, more I am thinking, that a highly
customized for Ruby editor with macro functions would be the solution...


--
Posted via http://www.ruby-....