Some Ruby Features missing...

E

edlich

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
 
A

Adriano Ferreira

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
 
S

Stefano Crocco

Alle mercoled=EC 16 maggio 2007, (e-mail address removed) 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=20
instance variable @var. You can redefine it as you would with any other=20
method.

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

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

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

I hope this helps

Stefano
 
L

Lyle Johnson

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.
 
A

Adriano Ferreira

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
 
B

Bernhard Stoeckner

--nextPart4489846.VnLp862y0O
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

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&view=markup

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

Regards,
Bernhard

--nextPart4489846.VnLp862y0O
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iQIVAwUARktaIStHjRr5f7lzAQJibw/9FKiKXQwhDY45Yfq6Gfv4H9YstiWG8fBo
YODRqaDg86G/6gHipHszv2fLL2clkaVeSw7wGmfATkEgkeOAc7upDUMBQdFvM73P
bVlNXoUkqbkqgQzZOk/OsVArRnKP8oeeW9veV1SoseTz+Xi8Zr6WZvwIuxURCc84
dEMhwKOVQWGXIN8MqNZotaDobSvpJu6XXlVFWDrPeGRTsZgCJHRZ4HAm/JnAmdk2
5a6EHGFkxOzSVJ0TFca/VuxdFcIyu8tLS0y96/twbp7WiM5phU2n0zoVbm+EI5Ev
4+el4T1Np138F3mMLMAd8CCxt8GOlND94NuWghFczpvsf70CtSOZZ26qTfRw7eyy
wmKixA1hBXVvEGt/jv08vQquTxRr3HxUXNLBaaBIxiiZGJKAPaNm7WPgq3y3HLDE
an7AP4X9PP0JBLDPsY1kH7urW//c4CIqCEWDfhMxG4U+U2YZZu/OCnwEIOz91T6U
Tp13XOu0GVAsqRuMd10djjebZ1COMPj2SjQOY3fu9WR+2VEu1v6xaXQ2E2aBmG4D
0Lul61qhCCNVKPfnfrjHtewYvsNf3UH3+pKsT01wQ4qVoyhZndzxNieuNhgCzyp+
XzSiSVe3Lfkvg5xYSxzHg9sDbrUHNF2meSwWnwzkaF+YZ7IH2+yk8tKHwdcVYW9k
ApcZ84cmDds=
=4gfd
-----END PGP SIGNATURE-----

--nextPart4489846.VnLp862y0O--
 
D

Drew Olson

Adriano said:
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
 
D

Drew Olson

Drew said:
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
 
R

Robert Klemme

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
 
G

gga

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.
 
E

edlich

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.
 
E

edlich

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

Unfortunately this is not at runtime. The original class must be
untouched.
But there have been some postings before in this thread that showed
how to do this at runtime.
The method module_eval was a cool hint.

But thanks anyway!
Best
Stefan Edlich
 
J

James Britt

Robert said:
You can implement annotations yourself with some meta programming.

Or use the Facets (I think) lib.


Look at Og/Nitro. Much use of annotations.
 
A

Austin Ziegler

Unfortunately this is not at runtime.

That's not true.

class Foo; end
Foo.new.foo # raises an exception
class Foo; def foo; "hello"; end; end
Foo.new.foo # "hello"

If you want to modify just an object, then just do so:

class Bar; end
foo = Bar.new
foo.baz # exception
class << bar; def baz; "hello"; end; end
foo.baz # "hello"

Better:

module BazMethod; def baz; "hello"; end; end
foo = Bar.new
foo.extend(BazMethod)
foo.baz # "hello"

There is no separate compile time in Ruby.

-austin
 
E

edlich

Hi,
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??)

So partially I figured out for myself why a UML modelling tool for
Ruby
is not present yet.

The reason is not (as some here mentioned) that ruby code is so small
and ruby is so agile (although both is true) that nowone needs a
modelling tool.

The reason is more rubys dynamic nature.
* Types are not that detectable
* Variables can be changed and (re?)declared everywhere
* And ruby contains such cool / weird (?) things as instance-class
variables:
Have you ever seen this:
class << self; attr_accessor :sides end

No? Then have a look at this site
http://railstips.org/2006/11/18/class-and-instance-variables-in-ruby

So now I guess you have better understanding why your boss should pay
you
double income if you have to build a ruby UML modelling tool.

Best
Stefan E.
 
R

Rick DeNatale

Hi,


So partially I figured out for myself why a UML modelling tool for
Ruby
is not present yet.

The reason is not (as some here mentioned) that ruby code is so small
and ruby is so agile (although both is true) that nowone needs a
modelling tool.

That's probably the main reason. Actually Ruby's properties tend to
attract practitioners of agile development "methodologies" where the
focus is on developing and evolving code rather than superflous (and
often out-of-date/sync) artifacts like diagrams.

This means that the ruby community is less likely to be motivated to
write a UML tool.

The debate over the usefulness of case tools and diagrams in agile
development has been going on for quite some time, even pre-dating
terms like agile, or extreme programming, back in 1993 we were more
likely to call it rapid or iterative development.

http://www.informatik.uni-trier.de/~ley/db/conf/oopsla/oopsla92.html#DeNataleBCTT92
The reason is more rubys dynamic nature.
* Types are not that detectable
* Variables can be changed and (re?)declared everywhere
* And ruby contains such cool / weird (?) things as instance-class
variables:
Have you ever seen this:
class << self; attr_accessor :sides end

No? Then have a look at this site
http://railstips.org/2006/11/18/class-and-instance-variables-in-ruby

Written by someone who was admittedly a ruby newby at the time he wrote it.
So now I guess you have better understanding why your boss should pay
you
double income if you have to build a ruby UML modelling tool.

There's a difference between applying UML to Ruby and writing a UML
tool in Ruby. There are probably difficulties in modeling advance
Ruby features in UML, but that seems to me to be a deficiency in UML
rather than Ruby.

On the other hand there's nothing inherent in Ruby which make it
unsuitable for writing a UML tool should a rubyist with a motivation
to do so were found. Dynamic languages like Ruby make fine tools for
implementing such.

In fact Grady Booch wrote the original Rational ROSE in Smalltalk, but
it got most of its use by C++ programmers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,174
Latest member
BlissKetoACV
Top