Some Ruby Features missing

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

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

class Foo; end

=> nil

Foo.new

=> #<Foo:0x1001380c> <-- default stringification

class Foo; def to_s; "a Foo"; end end

=> nil

Foo.new

=> a Foo <-- customized stringification

···

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

quit

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

Stefan Edlich

Most of them seems like less of the requirement from ruby and more of a
finding the similar kind of environment which u r already familiar with.
Every language provides its own flavor of coding.... No point finding the
similarity of a static type here!
Everything can easily be implemented in ruby way.

···

On 5/17/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?

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

Stefan Edlich

--
सुर मैक्स
साॅफ़टवेयर इंजीनियर

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

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

···

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.

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.

class Foo
   attr_accessor :foo # here the conventional accessor is defined

irb* def foo # and here it is overriden

       puts "foo invoked"
       @foo
   end
end

=> nil

Foo.new.foo

foo invoked
=> nil

···

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

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

···

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?

5. Is there a (free) UML modelling Tool for Ruby on the market?

Why do you want this?

-> I need to have the architectural UML Overview!!

(How do all the gbig ruby projects do this??)

They don't!

To me, whenever someone brings up UML, it sounds like they are talking about
Waterfall. We Rubyists are in general very Agile developers and as such
don't do all that boring and soon-to-be-out-of-date complete up-front
infrastructure modelling :D.

That said, the Revolution Health team needs to write more about their
development practices.

Jason

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. :slight_smile:

Btw, what is your project about?

Kind regards

  robert

···

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

···

On May 16, 3:49 pm, edl...@gmail.com wrote:

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.

C.module_eval will modify it for all the objects of class C

if something singleton needed for the particular object... then it can be
done as

def c.var
  "something singleton"
end

c.var # => "something singleton"

whereas still maintaining

C.new(9).var # => 9

···

On 5/17/07, Stefano Crocco <stefano.crocco@alice.it> wrote:

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

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

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

I hope this helps

Stefano

--
sur

Adriano Ferreira wrote:

You're missing "to_s"

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

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

> 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-forum.com/\.

> 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. :slight_smile:

Object#to_s generates the String that puts uses.

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.

···

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.

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

···

On 16 Mai, 21:17, "Adriano Ferreira" <a.r.ferre...@gmail.com> wrote:

On 5/16/07, edl...@gmail.com <edl...@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.

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

> Foo.new.foo
foo invoked
=> nil

Robert Klemme wrote:

···

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.

Or use the Facets (I think) lib.

Look at Og/Nitro. Much use of annotations.

--
James Britt

"You harmonize; then you customize."
  - Wilson Pickett

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.

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
Class and Instance Variables In Ruby // RailsTips by John Nunemaker

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.

···

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

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

Drew Olson wrote:

Adriano Ferreira wrote:

You're missing "to_s"

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

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

> 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-forum.com/\.

Well I'll just come out and say it:

Please oh please do not try to do Java persistence in Ruby. Learn how to do
things the Ruby way.

For your specific example here, when using ActiveRecord, if there's a
database table with a field of "id", that field is automatically recognized
as the primary key. If the primary key isn't "id", then it's very simple to
tell AR what the field is called:

class MyModel < ActiveRecord::Base
  set_primary_key "model_id"
end

With Ruby and ActiveRecord, the philosophy is for one to design the database
and let Ruby figure out class attributes from the database. This is the
opposite of Java persistence (Hibernate), so you need to keep this in mind.

(personally, Hibernate drives me absolutely bonkers because of this).

So if you have specific questions about what to do in Ruby, or how to do
something, we'll be happy to answer it. You'll find that a lot of things you
have to do in Java are simply non-issues in Ruby.

Jason

···

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

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.