[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Some Ruby Features missing...

edlich

5/16/2007 6:49:00 PM

To start a new Ruby Project I need some more features that I know
partially from Java:

1. Does Ruby support Annotations? Or is there another cool way to do
this
without changing the class itself?
-> I need to tell reflection to do something special with the class.

2. Can I modify the getter of any instance var?!
-> E.g. if an attribute defines attr_accessor, can I change the getter
at
runtime to do magic aspect stuff around the real get call?

3. Can we define new Keywords in Ruby?
-> I am thinking of somthing like this: Define a block containing some
real code and some useless keywords. Then I want to pass the block,
e.g. convert it to_s and read the real code and the keywords.
The keywords itself shall be ignored by ruby but can be parsed by me.
(I always thought this would be a basic feature to build a DSL...
but obviously real gurus can do without this...)

5. Is there a (free) UML modelling Tool for Ruby on the market?
-> I need to have the architectural UML Overview!!
(How do all the gbig ruby projects do this??)

6. I am missing the java toString method in Ruby.
-> Isn't there an easy way to define an objects puts behaviour?

Thanks in advance
(and thanks for the cool language Matz!)

Stefan Edlich

15 Answers

Adriano Ferreira

5/16/2007 6:59:00 PM

0

On 5/16/07, edlich@gmail.com <edlich@gmail.com> wrote:
> To start a new Ruby Project I need some more features that I know
> partially from Java:
>
> 1. Does Ruby support Annotations? Or is there another cool way to do
> this
> without changing the class itself?
> -> I need to tell reflection to do something special with the class.
>
> 2. Can I modify the getter of any instance var?!
> -> E.g. if an attribute defines attr_accessor, can I change the getter
> at
> runtime to do magic aspect stuff around the real get call?
>
> 3. Can we define new Keywords in Ruby?
> -> I am thinking of somthing like this: Define a block containing some
> real code and some useless keywords. Then I want to pass the block,
> e.g. convert it to_s and read the real code and the keywords.
> The keywords itself shall be ignored by ruby but can be parsed by me.
> (I always thought this would be a basic feature to build a DSL...
> but obviously real gurus can do without this...)
>
> 5. Is there a (free) UML modelling Tool for Ruby on the market?
> -> I need to have the architectural UML Overview!!
> (How do all the gbig ruby projects do this??)
>
> 6. I am missing the java toString method in Ruby.
> -> Isn't there an easy way to define an objects puts behaviour?

You're missing "to_s"

$ irb
irb> class Foo; end
=> nil
irb> Foo.new
=> #<Foo:0x1001380c> <-- default stringification

irb> class Foo; def to_s; "a Foo"; end end
=> nil
irb> Foo.new
=> a Foo <-- customized stringification

irb> quit

> Thanks in advance
> (and thanks for the cool language Matz!)
>
> Stefan Edlich
>
>
>

Stefano Crocco

5/16/2007 7:06:00 PM

0

Alle mercoledì 16 maggio 2007, edlich@gmail.com ha scritto:
> 2. Can I modify the getter of any instance var?!
> -> E.g. if an attribute defines attr_accessor, can I change the getter
> at
> runtime to do magic aspect stuff around the real get call?

attr_accessor :var simply defines a method called var which returns the
instance variable @var. You can redefine it as you would with any other
method.

class C
attr_accessor :var
def initialize value
@var = value
end
end

c = C.new 3
puts c.var
=>3

C.module_eval{def var; return 2;end}
c.var => 2

I hope this helps

Stefano

Lyle Johnson

5/16/2007 7:11:00 PM

0

On 5/16/07, edlich@gmail.com <edlich@gmail.com> wrote:

> 1. Does Ruby support Annotations? Or is there another cool way to do
> this
> without changing the class itself?
> -> I need to tell reflection to do something special with the class.

We might be able to better address this question if you could say what
it is that you're trying to accomplish.

Adriano Ferreira

5/16/2007 7:17:00 PM

0

On 5/16/07, edlich@gmail.com <edlich@gmail.com> wrote:
> 2. Can I modify the getter of any instance var?!
> -> E.g. if an attribute defines attr_accessor, can I change the getter
> at
> runtime to do magic aspect stuff around the real get call?

As Stefano said, you can modify the methods of a class in Ruby anytime you want.

irb> class Foo
irb> attr_accessor :foo # here the conventional accessor is defined
irb>
irb* def foo # and here it is overriden
irb> puts "foo invoked"
irb> @foo
irb> end
irb> end
=> nil

irb> Foo.new.foo
foo invoked
=> nil

Bernhard 'elven' Stoeckner

5/16/2007 7:23:00 PM

0

On Wednesday 16 May 2007, edlich@gmail.com wrote:
> To start a new Ruby Project I need some more features that I know
> partially from Java:
>
> 1. Does Ruby support Annotations? Or is there another cool way to do
> this
> without changing the class itself?
> -> I need to tell reflection to do something special with the class.
>
> 2. Can I modify the getter of any instance var?!
> -> E.g. if an attribute defines attr_accessor, can I change the getter
> at
> runtime to do magic aspect stuff around the real get call?
>

http://svn.swordcoast.net/v.cgi/circus/trunk/src/Hookable.rb?revision=2&v...

This might be some part of what you are looking for.

Regards,
Bernhard

Drew Olson

5/16/2007 7:24:00 PM

0

Adriano Ferreira wrote:
> You're missing "to_s"
>
> $ irb
> irb> class Foo; end
> => nil
> irb> Foo.new
> => #<Foo:0x1001380c> <-- default stringification
>
> irb> class Foo; def to_s; "a Foo"; end end
> => nil
> irb> Foo.new
> => a Foo <-- customized stringification
>
> irb> quit

This is a little nit-picky, but this code won't actually work unless
you've modified irb (as least it doesn't for me). As far as I know, irb
evaluates each line and prints the result of calling the #inspect method
on the return of the line. If we wanted to demonstrate the result of the
new #to_s method, we would have to attempt to print our foo object,
therefore implicitly calling #to_s. Like so:

irb(main):001:0> class Foo;end
=> nil
irb(main):002:0> myfoo = Foo.new
=> #<Foo:0x2829310>
irb(main):003:0> puts myfoo
#<Foo:0x2829310>
=> nil
irb(main):004:0> class Foo;def to_s;"hello from foo";end end
=> nil
irb(main):005:0> puts myfoo
hello from foo
=> nil

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

Drew Olson

5/16/2007 7:28:00 PM

0

Drew Olson wrote:
> Adriano Ferreira wrote:
>> You're missing "to_s"
>>
>> $ irb
>> irb> class Foo; end
>> => nil
>> irb> Foo.new
>> => #<Foo:0x1001380c> <-- default stringification
>>
>> irb> class Foo; def to_s; "a Foo"; end end
>> => nil
>> irb> Foo.new
>> => a Foo <-- customized stringification
>>
>> irb> quit
>
> This is a little nit-picky, but this code won't actually work unless
> you've modified irb

Whoops, I take that back, I was unaware that creating an instance of an
object called #to_s in irb. However, I think demonstrating printing an
instance makes a bit more sense here. My bad!

-Drew


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

Robert Klemme

5/17/2007 11:29:00 AM

0

On 16.05.2007 20:49, edlich@gmail.com wrote:
> To start a new Ruby Project I need some more features that I know
> partially from Java:
>
> 1. Does Ruby support Annotations? Or is there another cool way to do
> this
> without changing the class itself?
> -> I need to tell reflection to do something special with the class.

You can implement annotations yourself with some meta programming.

class Module
def annotate(meth, *info, &code)
(@annotations || = Hash.new {|h,k| h[k]})[meth]=[info, code]
end
end

class Foo
annotate :bar, "useful method"
annotate :foo do
3+4
end
end

And later do with @annotations whatever you want.

> 2. Can I modify the getter of any instance var?!
> -> E.g. if an attribute defines attr_accessor, can I change the getter
> at
> runtime to do magic aspect stuff around the real get call?

Yes. You can even redefine attr_accessor to generate different
accessors in the first place although that's not a good idea as this
will affect a lot of code. But you can define your own attr_accessor to
generate whatever code you need.

> 3. Can we define new Keywords in Ruby?
> -> I am thinking of somthing like this: Define a block containing some
> real code and some useless keywords. Then I want to pass the block,
> e.g. convert it to_s and read the real code and the keywords.
> The keywords itself shall be ignored by ruby but can be parsed by me.
> (I always thought this would be a basic feature to build a DSL...
> but obviously real gurus can do without this...)

I am not sure what you are at here. You can always define methods for
classes that behave keyword like, and you can even store the block for
later reference

class Module
def ignored
@ignored || []
end
private
def ignore(&b)
(@ignored ||= []) << b
end
end

irb(main):052:0> class Foo
irb(main):053:1> ignore do
irb(main):054:2* 1 + 2
irb(main):055:2> end
irb(main):056:1> end
=> [#<Proc:0x7ff78ecc@(irb):53>]
irb(main):057:0> Foo.ignored.map {|code| code.call}
=> [3]

> 5. Is there a (free) UML modelling Tool for Ruby on the market?
> -> I need to have the architectural UML Overview!!
> (How do all the gbig ruby projects do this??)

Not that I'm aware of. But I'd be curious to learn. Please let us know
if you find something like this.

> 6. I am missing the java toString method in Ruby.
> -> Isn't there an easy way to define an objects puts behaviour?

I'm surprised that you missed that one. :-)

Btw, what is your project about?

Kind regards

robert

gga

5/17/2007 1:20:00 PM

0

On May 16, 3:49 pm, edl...@gmail.com wrote:
> To start a new Ruby Project I need some more features that I know
> partially from Java:
>
> 1. Does Ruby support Annotations? Or is there another cool way to do
> this without changing the class itself?
> -> I need to tell reflection to do something special with the class.
>

You probably need to specify what you are doing. In general, ruby
code, unlike Java, is run and executed everywhere, so, there's no need
for annotations.
You can use a simple class/DSL as an annotation like

# DSL class for annotations
class Annotation
def initialize(&block)
@h = {}
instance_eval(&block)
end

# simple catch-all method
def method_missing(m, *vals)
if vals.empty?
@h[m]
else
@h[m] = *vals
end
end
end

class A
# @annot is a hidden attribute of *class* A (not instances of A)
# gets run as A is parsed.
@annot = Annotation.new {
help 'hello'
othermetadata 2
}

# get to the attribute from outside class (if needed)
def self._annotation
@annot
end
end

p A._annotation
#<Annotation:0x2b39e7e50b68 @h={:help=>"hello"}>
p A._annotation.help
"hello"

> 2. Can I modify the getter of any instance var?!
> -> E.g. if an attribute defines attr_accessor, can I change the getter
> at runtime to do magic aspect stuff around the real get call?

Sure. It would not be Ruby if you couldn't. You might want to try
irb, btw.
>From a command console or shell, do:

class A
attr_accessor :x
def initialize
@x = 5
end
def x
15
end
end

a = A.new
a.x
=>15


>
> 3. Can we define new Keywords in Ruby?

No. But you can use regexps substitutions to more or less do what
you
mention. Also, DSLs in ruby are very powerful -- see the simple
Annotation DSL class above. Google for Ruby DSL. There's an
excellent article at artima about it.
There's also libraries like erb and similar that already have created
their own syntax.
You can't, however, access the code of a block (what in ruby is known
as a block, btw) once the parser reads the block, thou.

> 5. Is there a (free) UML modelling Tool for Ruby on the market?
> -> I need to have the architectural UML Overview!!
> (How do all the gbig ruby projects do this??)

Good coding. For the most part, you'll find ruby code is often
several times smaller than Java or C++, so the need for UML is less.
Also, ri and corresponding web pages often will answer any sort of
question about any library.
rdoc can also create web pages with some basic read-only UML
relationships in the web docs (albeit noone seems to use it).
There's several free uml modelling tools, all of which can be used
with any language, but none that creates classes automatically from
source code a la latest Visual Studio, which I assume is what you
want.

>
> 6. I am missing the java toString method in Ruby.
> -> Isn't there an easy way to define an objects puts behaviour?
>

to_s.
Also, there's "inspect", which gives an overview of the internals of a
class, regardless of what the string representation looks like.

class A
def initialize
@x = 'asdsd'
end

def to_s
'crapola'
end
end

a = A.new
puts a
puts a.inspect # or just "p a"

There's also to_i, to_f, to_a, etc. where it makes sense for integer,
float, and array conversion.

edlich

5/17/2007 3:59:00 PM

0

On 16 Mai, 21:10, "Lyle Johnson" <lyle.john...@gmail.com> wrote:
> On 5/16/07, edl...@gmail.com <edl...@gmail.com> wrote:
>
> > 1. Does Ruby support Annotations? Or is there another cool way to do
> > this
> > without changing the class itself?
> > -> I need to tell reflection to do something special with the class.
>
> We might be able to better address this question if you could say what
> it is that you're trying to accomplish.

Well, I am sure you know 1000 areas for Annotations (as UML
Stereotypes, like
let the User decide what any program should do with your class or your
instances
without affecting the original class.)
One good example is to mark a class with "@index" to create an index
if this
class is stored in a database.

Any idea how to perform this?

Thanks!
Stefan E.