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.