[EVALUATION] - E03 - jamLang Evaluation Case Applied to Ruby

I

Ilias Lazaridis

Ilias said:
[EVALUATION] - E02 - Nitro, a Ruby Based WebFramework
http://groups-beta.google.com/group/comp.lang.ruby/msg/0fb8b0824d4fbaec

-

The above thread showed finally the need to split the evaluation down
into smaller chunks.

Here is a simple evaluation template (first part) which can be applied
to the Ruby language:

http://lazaridis.com/case/lang/index.html

If you like, please post the most elegant solutions (either to sections
or to the whole document).

I will collect the results and write them down in a document, which will
compare ruby with other languages.

This document can serve as an flash-start (for people which simply like
to take a look on ruby).

http://lazaridis.com/case/lang/ruby.html

To save some time, I would like to reverse the process (first the ruby
result, then the general template):

I've reviewed a little the documentation, but find nothing about metadata.

Is there a standard way to apply metadata/annotations to my class
Talker, its Methods, its Attributes?

E.g.:

class Talker

attr_accessor :name # Type = String; Label = Name; Size = 30;
attr_accessor :age # Type = int; Label = Age; Min=1; Max=150;

def sayYourName
end
end

-

"attr_accessor" does not work on class variables (e.g. @@count).

Must I create @@var/getter/setter manually?

..
 
J

James Edward Gray II

I've reviewed a little the documentation, but find nothing about
metadata.

Probably because it's too general a topic. The word "metadata" changes
meaning by context.
Is there a standard way to apply metadata/annotations to my class
Talker, its Methods, its Attributes?

E.g.:

class Talker

attr_accessor :name # Type = String; Label = Name; Size = 30;
attr_accessor :age # Type = int; Label = Age; Min=1; Max=150;

def sayYourName
end
end

Your comment has already hit on one solution. :) There may be many
others, depending on how you intend to use this information...
-

"attr_accessor" does not work on class variables (e.g. @@count).

Must I create @@var/getter/setter manually?

Not exactly. Here's a trick:

irb(main):001:0> class MyClass
irb(main):002:1> class << self
irb(main):003:2> attr_accessor :test
irb(main):004:2> end
irb(main):005:1> end
=> nil
irb(main):006:0> MyClass.test = 501
=> 501
irb(main):007:0> MyClass.test
=> 501

There is a gotcha though:

irb(main):008:0> class MyClass
irb(main):009:1> def test_method
irb(main):010:2> @@test
irb(main):011:2> end
irb(main):012:1> end
=> nil
irb(main):013:0> MyClass.new.test_method
NameError: uninitialized class variable @@test in MyClass
from (irb):10:in `test_method'
from (irb):14
from :0

You can see that this technique does not use @@test.

All of this is just playing around though. If you seriously need to
define class accessors a lot and can't be bothered to type:

def self.test( ) @@test end
def self.test=( value ) @@test = value end

Most editors I've ever seen will allow you to macro that and assign it
to a single keystroke.

My opinion, feel free to completely ignore, is that you've strayed
pretty far from "evaluating Ruby" when you worry about details like
this. We can sit here and show you examples like the above
indefinitely, exploring all corners of the language. There's no
substitute for just learning the language and seeing what it can do for
you though, and this is a poor way to go about that.

James Edward Gray II
 
I

Ilias Lazaridis

James said:
Probably because it's too general a topic. The word "metadata" changes
meaning by context.
ok


Your comment has already hit on one solution. :) There may be many
others, depending on how you intend to use this information...

I extract: Ruby has not standard-mechanism for metadata/annotations.

ok
Not exactly. Here's a trick:
[...] - (trick's and workarounds)
Most editors I've ever seen will allow you to macro that and assign it
to a single keystroke.

I am aware about editors and macros.

-

Where can I find the implementation of "attr_accessor"?

I'm intrested in seeing how much effort it is to write an own
"flex_attr_accessor".

[I will possibly include this in the evaluation template, section
"language extension"]
My opinion, feel free to completely ignore, is that you've strayed
pretty far from "evaluating Ruby" when you worry about details like
this. We can sit here and show you examples like the above
indefinitely, exploring all corners of the language. There's no
substitute for just learning the language and seeing what it can do for
you though, and this is a poor way to go about that.

James Edward Gray II

I've an evaluation template, which I like to apply to several languages.

Ruby looks very nice till now, although I've hit already some limitations:

http://lazaridis.com/case/lang/ruby.html

..
 
J

James Edward Gray II

I extract: Ruby has not standard-mechanism for metadata/annotations.

ok

Correct, but it certainly provides ample tools to build whatever you
need.

This aspect you've just hit on is the very essence of programming, in
my opinion. No language provides everything for every need. The
processes of adapting your chosen language to the problem space is the
craft.
Where can I find the implementation of "attr_accessor"?

I'm intrested in seeing how much effort it is to write an own
"flex_attr_accessor".

I'm sure attr_accessor is defined in the Ruby source. Here's my
version of a pure Ruby class accessor:

irb(main):015:0> class Module
irb(main):016:1> def cattr_accessor( *symbols )
irb(main):017:2> symbols.each do |sym|
irb(main):018:3* class_eval "def self.#{sym}( ) @@#{sym} end
irb(main):019:3" def self.#{sym}=( value ) @@#{sym}
= value end"
irb(main):020:3> end
irb(main):021:2> end
irb(main):022:1> end
=> nil
irb(main):023:0> class Accessor
irb(main):024:1> cattr_accessor :eek:ne, :two
irb(main):025:1> def self.fetch_one( )
irb(main):026:2> @@one
irb(main):027:2> end
irb(main):028:1> end
=> nil
irb(main):029:0> Accessor.one = "James"
=> "James"
irb(main):030:0> Accessor.two = "Gray"
=> "Gray"
irb(main):031:0> Accessor.one
=> "James"
irb(main):032:0> Accessor.two
=> "Gray"
irb(main):033:0> Accessor.fetch_one
=> "James"

Hope that helps.

James Edward Gray II
 
I

Ilias Lazaridis

James said:
[...]
I extract: Ruby has not standard-mechanism for metadata/annotations.

ok

Correct, but it certainly provides ample tools to build whatever you need.

This aspect you've just hit on is the very essence of programming, in my
opinion. No language provides everything for every need. The processes
of adapting your chosen language to the problem space is the craft.

I'm just writing the situation down.

personally to me, language-efficiency is more important than a
standartization [which can btw. limit efficiency, see JAVA].
Where can I find the implementation of "attr_accessor"?

I'm intrested in seeing how much effort it is to write an own
"flex_attr_accessor".

I'm sure attr_accessor is defined in the Ruby source.
ok

Here's my version of a pure Ruby class accessor:

irb(main):015:0> class Module
irb(main):016:1> def cattr_accessor( *symbols ) [...]

Hope that helps.

yes, very nice!

looks like a "macro", written in ruby, made available to a project by
simply including the file.

very nice!
James Edward Gray II

..
 
I

Ilias Lazaridis

Ilias Lazaridis wrote:
[...]
I will collect the results and write them down in a document, which will
compare ruby with other languages.

This document can serve as an flash-start (for people which simply like
to take a look on ruby).

http://lazaridis.com/case/lang/ruby.html

So, the last question for this part:

how can I add runtime-accessible [meta]data to a ruby function definition?

Any standard to do this?

Or any suggestions?

..
 
R

Robert Klemme

Ilias Lazaridis said:
Ilias Lazaridis wrote:
[...]
I will collect the results and write them down in a document, which will
compare ruby with other languages.

This document can serve as an flash-start (for people which simply like
to take a look on ruby).

http://lazaridis.com/case/lang/ruby.html

So, the last question for this part:

how can I add runtime-accessible [meta]data to a ruby function definition?

Any standard to do this?

Or any suggestions?

Put a hash into the class with symbol as key and whatever meta data you
need. Add some syntactic sugar and you're done. I'm quite sure someone has
done that already (maybe even on RAA).

Kind regards

robert
 
I

Ilias Lazaridis

Robert said:
Ilias Lazaridis wrote:
[...]
I will collect the results and write them down in a document, which
will compare ruby with other languages.

This document can serve as an flash-start (for people which simply
like to take a look on ruby).

http://lazaridis.com/case/lang/ruby.html

So, the last question for this part:

how can I add runtime-accessible [meta]data to a ruby function
definition?

Any standard to do this?
?
Or any suggestions?

Put a hash into the class with symbol as key and whatever meta data you
need. Add some syntactic sugar and you're done. I'm quite sure someone
has done that already (maybe even on RAA).

I don't understand.

def talker

def sayHello
puts "Hello World"
end

def sayYourName
puts @name
end

end

how do I put a "hash" which keeps metadata to each _function_?
 
I

Ilias Lazaridis

Jacob said:
No. To my knowledge there is only the one comment syntax in ruby.
ok


Sure. #{thisMethodName} in a string literal simply calls .to_s on the
enclosed expression and inserts it at the designated location. So we
can pull that out such:

puts thisMethodName.to_s + ": Hello World"

ok.

I like this contruct more:

"puts "#{thisMethodName}: Hello World"

but the usage of "#" is missleading (comment marker)
The + operator performs concatenation when its arguments are strings.

Jacob Fugal

..
 
I

Ilias Lazaridis

Ilias said:
[EVALUATION] - E02 - Nitro, a Ruby Based WebFramework
http://groups-beta.google.com/group/comp.lang.ruby/msg/0fb8b0824d4fbaec

-

The above thread showed finally the need to split the evaluation down
into smaller chunks.

Here is a simple evaluation template (first part) which can be applied
to the Ruby language:

http://lazaridis.com/case/lang/index.html

If you like, please post the most elegant solutions (either to sections
or to the whole document).

I will collect the results and write them down in a document, which will
compare ruby with other languages.

This document can serve as an flash-start (for people which simply like
to take a look on ruby).

http://lazaridis.com/case/lang/ruby.html

I hope you like the results so far.

I need to fill in the interactive session, to close this thread:

http://lazaridis.com/case/lang/index.html#run_interactive

http://lazaridis.com/case/lang/ruby.html#run_interactive

..
 
L

Luc Heinrich

Ilias Lazaridis said:
how does a mac-user install ruby?

A mac user does not install Ruby, because Mac OS X already has it
pre-installed. Macs are better, everybody knows this... :>

However, if a mac user wants the latest and greatest, he usually
downloads the source tarball, go to the terminal and type:

% ./configure
% make
% sudo make install

Boom, done (ok, you might want to make sure your $PATH gets you to the
new one instead of the default one, but that's pretty much it).
 
I

Ilias Lazaridis

Luc said:
A mac user does not install Ruby, because Mac OS X already has it
pre-installed.
intresting.

Macs are better, everybody knows this... :>

However, if a mac user wants the latest and greatest, he usually
downloads the source tarball, go to the terminal and type:

% ./configure
% make
% sudo make install

Boom, done (ok, you might want to make sure your $PATH gets you to the
new one instead of the default one, but that's pretty much it).

I would expect this update sequence (from a better System):

macup ruby latest_release

-

ok, I extract that I don't have to include download instructions for MAC
users.

..
 
R

Robert Klemme

Ilias Lazaridis said:
Robert said:
Ilias Lazaridis wrote:
[...]

I will collect the results and write them down in a document, which
will compare ruby with other languages.

This document can serve as an flash-start (for people which simply like
to take a look on ruby).

http://lazaridis.com/case/lang/ruby.html

So, the last question for this part:

how can I add runtime-accessible [meta]data to a ruby function
definition?

Any standard to do this?
?
Or any suggestions?

Put a hash into the class with symbol as key and whatever meta data you
need. Add some syntactic sugar and you're done. I'm quite sure someone
has done that already (maybe even on RAA).

I don't understand.

def talker

def sayHello
puts "Hello World"
end

def sayYourName
puts @name
end

end

how do I put a "hash" which keeps metadata to each _function_?

class Module
def meta() @META ||= {} end
end

class Foo
def bar() end
meta[:bar] = "bar_meta"
end
bar_meta
=> nil

Regards

robert
 
I

Ilias Lazaridis

Robert Klemme wrote:
[...]
So, the last question for this part:

how can I add runtime-accessible [meta]data to a ruby function
definition?
[...]
I don't understand.

def talker

def sayHello
puts "Hello World"
end

def sayYourName
puts @name
end

end

how do I put a "hash" which keeps metadata to each _function_?

class Module
def meta() @meta ||= {} end
end

class Foo
def bar() end
meta[:bar] = "bar_meta"
end
[...]

the above is essentially "class metadata".

but it gives me the basic idea:

class Object
def meta() @meta ||= {} end
end

talker.sayYourName.meta[:author] = "it's just me"
puts talker.sayYourName.meta[:author]
=> it's just me

[i like this language *very* much]

-

But to understand this fully, can someone please decrypt this:

def meta()
@meta ||= {}
end

..
 
A

Anders Engström

On Sun, Apr 03, 2005 at 08:39:42PM +0900, Ilias Lazaridis wrote:
[snipp]
But to understand this fully, can someone please decrypt this:

def meta()
@meta ||= {}
end

That's lazy initialization of the @meta variable. Another (and more
verbose) way to write it would be:

def meta()
unless @meta #If not defined, set the value
@meta = {}
end
@meta # Return Meta
end

"@meta ||= {}" evaluates to "@meta = @meta || {}" and since the returned
value from a method is the last evaluated expression "@meta ||= {}"
works.

//Anders
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top