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…)

  4. 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??)

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

On 5/16/07, [email protected] [email protected] wrote:

at
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??)

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

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/16/07, [email protected] [email protected] 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.

Alle mercoledì 16 maggio 2007, [email protected] ha scritto:

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

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/16/07, [email protected] [email protected] wrote:

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

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

On Wednesday 16 May 2007, [email protected] wrote:

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

Drew O. wrote:

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

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

On 16.05.2007 20:49, [email protected] 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.

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

  1. 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:[email protected]:53(irb)]
irb(main):057:0> Foo.ignored.map {|code| code.call}
=> [3]

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

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

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

On May 16, 3:49 pm, [email protected] 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”

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

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

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

  1. 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 16 Mai, 21:10, “Lyle J.” [email protected] wrote:

On 5/16/07, [email protected] [email protected] 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.

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

Robert K. wrote:

On 16.05.2007 20:49, [email protected] 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 B.

“You harmonize; then you customize.”

  • Wilson Pickett

On 16 Mai, 21:17, “Adriano F.” [email protected] wrote:

irb> attr_accessor :foo # here the conventional accessor is defined
=> 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

On 5/17/07, [email protected] [email protected] wrote:

irb> end
=> nil

irb> Foo.new.foo
foo invoked
=> nil
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

Hi,

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs