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

Ilias Lazaridis wrote:

Ilias Lazaridis wrote:

[...]

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

I've tried this:

open command window
change to project directory
cmd: irb
within irb
cmd: require 'talker'

talker is created, produces output.

but: the "talker" instance which was created during read in (of
talker.rb) has died.

why?

Local variables evaluated during a require() are not accessible
from outside the file; you may consider this such that the file
is the scope within which the variable is defined, just like in
a method body.

how do i read-in files, which produce instances, which 'survive'?

Global variables (although this practice would be discouraged),
which are identified by a $-prefix, e.g. $global = 'something'.
Come to think of it, constants may also be accessible.

Normally one will have either an accessor method of some sort
or simply provide the means of _creating_ an instance.

and: how do I delete an instance? [tried: del, delete, kill, rm, free]

One cannot explicitly delete an instance. It will be 'deleted'
(marked for garbage collection) once it is without references.

E

No-one expects the Solaris POSIX implementation!

···

Le 3/4/2005, "Ilias Lazaridis" <ilias@lazaridis.com> a écrit:

Douglas Livingstone wrote:

···

On Mon, 21 Mar 2005 02:59:53 +0900, Ilias Lazaridis <ilias@lazaridis.com> wrote:

Martin DeMello wrote:

Ilias Lazaridis <ilias@lazaridis.com> wrote:

# __FILE__ == $0 means that the program is being run directly
# rather than 'require'd from another file

if __FILE__ == $0
talker = Talker.new
talker.sayHello
end

Assuming I placethe code into the file "talker.rb".

from the command-line, i like to start it, e.g. with "ruby talker.rb"

I miss a "main" directive / function / object.

You can think of the whole file as "main". The code will be read from
top to bottom.

I understand.

This means that ruby is _not_ strictly Object Oriented.

And this means that I can drop the "if __FILE__ [...]" construct.

I like both points.

-

I assume I can launch my application with "ruby talker.rb".

# one-pass interpreter, and you can reopen classes
# so let's just continue

[sidenote: I don't understand this]

Example:

# first time setting the class
class Talker
  attr_accessor :name
  
  def initialize(name)
    @name = name
  end
  
end

talker = Talker.new('Bob')
puts talker.name

# reopen the class to add sayYourName
class Talker
  def sayYourName
    puts @name
  end
end

talker.sayYourName

The output is:

Bob

[I think I understand, but will postpone this construct]

class Talker
attr_accessor :name, :age

can I write?:

attr_accessor :name
attr_accessor :age

yes

ok

  def initialize(name, age)
    @name, @age = name, age
  end

Is this the constructor?

I assume I can write

   def initialize(name, age)
     @name = name
     @age = age
   end

yes

ok

# following the spec, though say_name is more rubyish
def sayYourName
   puts @name
end

can I write?: def sayYourName puts @name end

You need to add semicolons if you want to put more than one line on a line:

def sayYourName; puts @name; end

ok

hth,
Douglas

..

--
http://lazaridis.com

Martin DeMello wrote:

···

Ilias Lazaridis <ilias@lazaridis.com> wrote:

# one-pass interpreter, and you can reopen classes
# so let's just continue

[sidenote: I don't understand this]

The ruby interpreter is one-pass insofar as it takes a single pass through

[...] - (interpreter, dynamic mixins, ...)

Thank you for you explanations.

I've understood now everything.

Altough I'm excited about some contructs, I must postpone their discussion.

I'll come back to them soon.

..

--
http://lazaridis.com

Ruby has a multiline comment syntax:

=begin
  This is a comment.
=end

Just FYI.

James Edward Gray II

···

On Mar 24, 2005, at 12:19 PM, Jacob Fugal wrote:

"#" is used as a comment marker _and_ partly within code.

Can I use another comment marker?

No. To my knowledge there is only the one comment syntax in ruby.

Can I use another comment marker?

No. To my knowledge there is only the one comment syntax in ruby.

You can use e.g.

=begin
bla bla
bla
=end

martinus

Ilias Lazaridis wrote:

Where can I find the following information?:

  * An UML diagramm (or another visual representation) of the ruby class-model.

I've generated this one automatically:

http://flgr.0x42.net/class-graph.png

Note that it is quite large and not too pretty, though.

  * A deep (but compact) description of the reflection/introspection api's.

Well, the Pickaxe book (slightly outdated version is available online for free, new issue with lots of new material can be ordered online) contains that among much other information and is a pretty interesting read. See

http://www.rubycentral.com/book/ (first issue)
Programming Ruby (first issue with Wiki for changes)
https://pragprog.com/titles/ruby/programming-ruby-2nd-edition/ (second issue)
http://www.amazon.com/exec/obidos/tg/detail/-/0974514055/ (second issue)

I'd recommend ordering the second issue from the pragmatic guys as it contains detailed information that will be useful for evaluating Ruby in depth.

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

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 :one, :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

···

On Mar 31, 2005, at 3:44 AM, Ilias Lazaridis wrote:

Ilias Lazaridis wrote:

I don't understand.

Does anybody else see an analogy to "Blind Man's Bluff" in this thread?

···

--
Glenn Parker | glenn.parker-AT-comcast.net | <http://www.tetrafoil.com/&gt;

Jacob Fugal wrote:
[...]

"#" is used as a comment marker _and_ partly within code.

Can I use another comment marker?

No. To my knowledge there is only the one comment syntax in ruby.

ok

Can I write "puts "#{thisMethodName}: Hello World"" in an different
manner, without the use of "#"?

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

..

···

On Thu, 24 Mar 2005 10:59:50 +0900, Ilias Lazaridis <ilias@lazaridis.com> wrote:

--
http://lazaridis.com

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

···

Ilias Lazaridis <ilias@lazaridis.com> wrote:

how does a mac-user install ruby?

--
Luc Heinrich - lucsky@mac.com

"Ilias Lazaridis" <ilias@lazaridis.com> schrieb im Newsbeitrag news:d2m0g0$j8q$1@usenet.otenet.gr...

Robert Klemme wrote:

"Ilias Lazaridis" <ilias@lazaridis.com> schrieb im Newsbeitrag news:d2lm17$2go$1@usenet.otenet.gr...

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

puts Foo.meta[:bar]

bar_meta
=> nil

Regards

    robert

···

Kind regards

   robert

--
http://lazaridis.com

Saynatkari wrote:

Ilias Lazaridis wrote:

[...]

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

I've tried this:

open command window
change to project directory
cmd: irb
within irb
cmd: require 'talker'

talker is created, produces output.

but: the "talker" instance which was created during read in (of
talker.rb) has died.

why?

Local variables evaluated during a require() are not accessible
from outside the file; you may consider this such that the file
is the scope within which the variable is defined, just like in
a method body.

I understand.

instead of 'require', can I use another command, like e.g. "expand".

how do i read-in files, which produce instances, which 'survive'?

Global variables (although this practice would be discouraged),
which are identified by a $-prefix, e.g. $global = 'something'.
Come to think of it, constants may also be accessible.

ok

Normally one will have either an accessor method of some sort
or simply provide the means of _creating_ an instance.

and: how do I delete an instance? [tried: del, delete, kill, rm, free]

One cannot explicitly delete an instance. It will be 'deleted'
(marked for garbage collection) once it is without references.

how can i reverse the process?

I like to kill the object, thus the destructor is called.

the references will point to nil.

..

···

Le 3/4/2005, "Ilias Lazaridis" <ilias@lazaridis.com> a écrit:

--
http://lazaridis.com

You can think of the whole file as "main". The code will be read from
top to bottom.

[...]

This means that ruby is _not_ strictly Object Oriented.

Ruby is not strictly OO, but not for the reason you think. Here is a nice explenation why:
http://www.rubycentral.com/book/classes.html#S3

And this means that I can drop the "if __FILE__ [...]" construct.

__FILE__ is the name of the current source file.
$0 is the name of the top-level ruby program being executed.

So this construct just checks if this the source file is the file that is executed directly with e.g. 'ruby talker.rb', and only if this is the case, the codeblock will be executed.

martinus

Florian Gross wrote:

Ilias Lazaridis wrote:

Where can I find the following information?:

  * An UML diagramm (or another visual representation) of the ruby class-model.

I've generated this one automatically:

http://flgr.0x42.net/class-graph.png

with which tool have you generated this?

Note that it is quite large and not too pretty, though.

This is a nice overview.

Does anyone has the detailed model of the core class-model?

  * A deep (but compact) description of the reflection/introspection api's.

Well, the Pickaxe book (slightly outdated version is available online for free, new issue with lots of new material can be ordered online) contains that among much other information and is a pretty interesting read. See

http://www.rubycentral.com/book/ (first issue)
Programming Ruby (first issue with Wiki for changes)
https://pragprog.com/titles/ruby/programming-ruby-2nd-edition/ (second issue)
http://www.amazon.com/exec/obidos/tg/detail/-/0974514055/ (second issue)

I'd recommend ordering the second issue from the pragmatic guys as it contains detailed information that will be useful for evaluating Ruby in depth.

thank you for the information.

I need at this point only reference of the reflection/introspection api.

I cannot locate it.

..

···

--
http://lazaridis.com

James Edward Gray II wrote:

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

[...]

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

..

···

On Mar 31, 2005, at 3:44 AM, Ilias Lazaridis wrote:

--
http://lazaridis.com

Luc Heinrich wrote:

how does a mac-user install ruby?

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

···

Ilias Lazaridis <ilias@lazaridis.com> wrote:

-

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

..

--
http://lazaridis.com

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

..

--
http://lazaridis.com

Martin Ankerl wrote:

You can think of the whole file as "main". The code will be read from
top to bottom.

[...]

This means that ruby is _not_ strictly Object Oriented.

Ruby is not strictly OO, but not for the reason you think. Here is a nice explenation why:
http://www.rubycentral.com/book/classes.html#S3

impressive.

And this means that I can drop the "if __FILE__ [...]" construct.

__FILE__ is the name of the current source file.

ok, here i guessed right (intuitive naming).

$0 is the name of the top-level ruby program being executed.

here i thought that "$0" represents "NULL".

$0 => main-file (or entry-file, or top-level file, or top-level program)

So this construct just checks if this the source file is the file that is executed directly with e.g. 'ruby talker.rb', and only if this is the case, the codeblock will be executed.

Ok, now it's clear.

martinus

..

···

--
http://lazaridis.com

Ilias Lazaridis wrote:

I've generated this one automatically:

http://flgr.0x42.net/class-graph.png

with which tool have you generated this?

Graphviz and a custom Ruby script which uses Ruby's introspection for finding out the class and module relationships.

[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

···

On Sun, Apr 03, 2005 at 08:39:42PM +0900, Ilias Lazaridis wrote:

--
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. Anders Engström aengstrom@gnejs.net
. http://www.gnejs.net PGP-Key: ED010E7F
. [Your mind is like an umbrella. It doesn't work unless you open it.]