Google Groups Home
Help | Sign in
- E03 - jamLang Evaluation Case Applied to Ruby
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  Messages 1 - 25 of 74 - Collapse all  -  Translate all to Translated (View all originals)   Newer >
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Ilias Lazaridis  
View profile  
 More options Mar 18 2005, 9:37 am
Newsgroups: comp.lang.ruby
From: Ilias Lazaridis <il...@lazaridis.com>
Date: Fri, 18 Mar 2005 16:37:36 +0200
Local: Fri, Mar 18 2005 9:37 am
Subject: [EVALUATION] - E03 - jamLang Evaluation Case Applied to Ruby
[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

.

--
http://lazaridis.com


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Martin DeMello  
View profile  
 More options Mar 18 2005, 11:29 am
Newsgroups: comp.lang.ruby
From: Martin DeMello <martindeme...@yahoo.com>
Date: Fri, 18 Mar 2005 16:29:33 GMT
Local: Fri, Mar 18 2005 11:29 am
Subject: Re: [EVALUATION] - E03 - jamLang Evaluation Case Applied to Ruby

Ilias Lazaridis <il...@lazaridis.com> wrote:

> 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

Okay, that's a neat bit of work. Here's a quick solution (ruby makes
most of this fairly trivial):

# here's your basic talker class

class Talker
  def sayHello
    puts "Hello world"
  end
end

# __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

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

# here are the two instance variables added to the class, complete with
# autogenerated getters and setters

class Talker
  attr_accessor :name, :age

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

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

  def sayYourAge
    puts @age
  end
end

# now note that our object 'talker' has access to the new methods in its
# class

if __FILE__ == $0
  # note that we aren't breaking encapsulation and accessing the vars directly;
  # the setter methods are called name= and age=, and the getters are called
  # name and age
  talker.name = "Just another talker"
  talker.age = 1
  talker.sayYourName
  talker.sayYourAge
end

# reflection
class Talker

  # simple

  def sayYourClassName
    puts self.class.name # the 'self' is optional here
  end

  # objects know nothing about the variables that are bound to them
  # (a variable name is not the name of the instance anyway). The
  # closest you can come to the "name" of an object is it's object id, so...
  def sayYourInstanceName
    puts object_id # again, you could say self.object_id if you prefer
  end

  # advanced

  # caller returns a stack (array) of strings of the form
  #   file:linenumber in `method'
  # so we extract the most recent one and parse the method name out
  # code from PLEAC
  def thisMethodName
    caller[0] =~ /in `([^']+)'/ ? $1 : '(anonymous)';
  end

  # string interpolation in action - the bit between the #{} can be
  # any valid expression; to_s will be called on the result and
  # interpolated into the string
  def sayHelloAdvanced
    puts "#{thisMethodName}: Hello World"
  end

  # expert
  def sayYourClassDefinition
    puts "Class:"
    sayYourClassName

    # %{} is another way to write a string literal
    # (looks neat for multiline strings)
    # we use the standard 'inspect' method to print out arrays of
    # method names in a ["meth1", "meth2", ...] format
    puts %{
Methods:
  public:
    #{public_methods.inspect}
  protected
    #{protected_methods.inspect}
  private:
    #{private_methods.inspect}
  non-inherited:
    #{(methods - self.class.superclass.instance_methods).inspect}

  Instance Variables:
    #{instance_variables.inspect}
    }

    # note that the instance variables belong to the *instance*, not
    # to the class, so they're not technically part of the class
    # definition. the following code is illustrative:
    #
    # class A
    #   attr_accessor :foo, :bar # defines getters and setters
    # end
    #
    # a = A.new
    # p a.instance_variables # => []
    # a.foo = 10
    # p a.instance_variables # => ["@foo"]
    # b = A.new
    # p b.instance_variables # => []
  end

  def sayYourClassCode
    puts "Sadly, you cannot introspect the source code from within a program"
    # though see http://rubyforge.org/projects/parsetree/
    # for a way to get at the parsed AST
  end
end

# testing it out

if __FILE__ == $0
  talker.sayHelloAdvanced
  talker.sayYourClassName
  talker.sayYourClassDefinition
  talker.sayYourClassCode
end

Hope this helps.

martin


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ilias Lazaridis  
View profile  
 More options Mar 20 2005, 12:58 pm
Newsgroups: comp.lang.ruby
From: Ilias Lazaridis <il...@lazaridis.com>
Date: Sun, 20 Mar 2005 19:58:37 +0200
Local: Sun, Mar 20 2005 12:58 pm
Subject: Re: [EVALUATION] - E03 - jamLang Evaluation Case Applied to Ruby

Martin DeMello wrote:
> Ilias Lazaridis <il...@lazaridis.com> wrote:

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

> Okay, that's a neat bit of work. Here's a quick solution (ruby makes
> most of this fairly trivial):

I apologize for the late answer.

> # here's your basic talker class

> class Talker
>   def sayHello
>     puts "Hello world"
>   end
> end

ok

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

something like:

def main
    talker = Talker.new
    talker.sayHello
end

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

[sidenote: I don't understand this]

> # here are the two instance variables added to the class, complete with
> # autogenerated getters and setters

very nice!

> class Talker
>   attr_accessor :name, :age

can I write?:

attr_accessor :name
attr_accessor :age

 >   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

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

can I write?: def sayYourName puts @name end

>   def sayYourAge
>     puts @age
>   end
> end

ok

> # now note that our object 'talker' has access to the new methods in its
> # class

> if __FILE__ == $0
>   # note that we aren't breaking encapsulation and accessing the vars directly;
>   # the setter methods are called name= and age=, and the getters are called
>   # name and age

very(!) nice!

>   talker.name = "Just another talker"
>   talker.age = 1
>   talker.sayYourName
>   talker.sayYourAge
> end

ok

> # reflection

[...]

seperate message will follow.

.

--
http://lazaridis.com


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Douglas Livingstone  
View profile  
 More options Mar 20 2005, 7:07 pm
Newsgroups: comp.lang.ruby
From: Douglas Livingstone <ramp...@gmail.com>
Date: Mon, 21 Mar 2005 09:07:25 +0900
Local: Sun, Mar 20 2005 7:07 pm
Subject: Re: [EVALUATION] - E03 - jamLang Evaluation Case Applied to Ruby

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

> > # 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
Bob

> > class Talker
> >   attr_accessor :name, :age

> can I write?:

> attr_accessor :name
> attr_accessor :age

yes

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

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

hth,
Douglas


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Martin DeMello  
View profile  
 More options Mar 21 2005, 4:58 am
Newsgroups: comp.lang.ruby
From: Martin DeMello <martindeme...@yahoo.com>
Date: Mon, 21 Mar 2005 09:58:05 GMT
Local: Mon, Mar 21 2005 4:58 am
Subject: Re: [EVALUATION] - E03 - jamLang Evaluation Case Applied to Ruby

Ilias Lazaridis <il...@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
the file, from top to bottom, building and executing an abstract syntax tree
as it goes. Anything enclosed in a def...end block (and a few other
delayed-evaluation constructs like lambda) gets converted into a 'callable'
block of code, the rest is just executed.  That's how attr_accessor works,
incidentally - it's just a piece of code that runs inside a class's context
and generates methods when it's executed.  Try the following:

class Foo
  def say_hello
    puts "hello world"
  end

  puts "hi, this is the reader"
end

a = Foo.new
a.say_hello

the first puts statement gets executed when say_hello is called; the second
one is not inside a def statement, and so gets executed when the interpreter
gets to it.

Hence the lack of a main() method - in a sense, the whole fine is a main(),
since the entry point is the top of the file (comes in real handy when
writing quick imperative scripts - you aren't forced to define a class just
because Ruby is a pure OO language).

Hence the if __FILE__ == $0 blocks - if the file is not being run directly,
the condition fails, and the whole block is skipped.

As for reopening classes, you can modify a class at runtime (add/delete
methods, mix in a module), and any objects belonging to the class inherit
those modifications (rather trivially, since they look up methods in the
class). But, since this is a one pass interpreter, those changes come into
effect at the time the reader reads them, and so the object "changes" in the
middle of its lifecycle.

martin


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ilias Lazaridis  
View profile  
 More options Mar 21 2005, 9:10 am
Newsgroups: comp.lang.ruby
From: Ilias Lazaridis <il...@lazaridis.com>
Date: Mon, 21 Mar 2005 16:10:56 +0200
Local: Mon, Mar 21 2005 9:10 am
Subject: Re: [EVALUATION] - E03 - jamLang Evaluation Case Applied to Ruby

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

[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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ilias Lazaridis  
View profile  
 More options Mar 21 2005, 9:30 am
Newsgroups: comp.lang.ruby
From: Ilias Lazaridis <il...@lazaridis.com>
Date: Mon, 21 Mar 2005 16:30:24 +0200
Local: Mon, Mar 21 2005 9:30 am
Subject: Re: [EVALUATION] - E03 - jamLang Evaluation Case Applied to Ruby
Martin DeMello wrote:
> Ilias Lazaridis <il...@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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Martin Ankerl  
View profile  
 More options Mar 21 2005, 9:38 am
Newsgroups: comp.lang.ruby
From: Martin Ankerl <martin.ank...@gmail.com>
Date: Mon, 21 Mar 2005 15:38:41 +0100
Local: Mon, Mar 21 2005 9:38 am
Subject: Re: [EVALUATION] - E03 - jamLang Evaluation Case Applied to Ruby

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ilias Lazaridis  
View profile  
 More options Mar 21 2005, 4:28 pm
Newsgroups: comp.lang.ruby
From: Ilias Lazaridis <il...@lazaridis.com>
Date: Mon, 21 Mar 2005 23:28:29 +0200
Local: Mon, Mar 21 2005 4:28 pm
Subject: Re: [EVALUATION] - E03 - jamLang Evaluation Case Applied to Ruby

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ilias Lazaridis  
View profile  
 More options Mar 22 2005, 8:38 pm
Newsgroups: comp.lang.ruby
From: Ilias Lazaridis <il...@lazaridis.com>
Date: Wed, 23 Mar 2005 03:38:11 +0200
Local: Tues, Mar 22 2005 8:38 pm
Subject: Re: [EVALUATION] - E03 - jamLang Evaluation Case Applied to Ruby

The first result is available.

I would like to setup ruby at this point, to add the installation steps
to the document.

Can one suggest me the simplest possible installation (one-click)?

One for Windows and one for Linux (if possible similar in content)?

.

--
http://lazaridis.com


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tom Copeland  
View profile  
 More options Mar 22 2005, 10:12 pm
Newsgroups: comp.lang.ruby
From: Tom Copeland <t...@infoether.com>
Date: Wed, 23 Mar 2005 12:12:05 +0900
Local: Tues, Mar 22 2005 10:12 pm
Subject: Re: [EVALUATION] - E03 - jamLang Evaluation Case Applied to Ruby

On Wed, 2005-03-23 at 10:39 +0900, Ilias Lazaridis wrote:
> Can one suggest me the simplest possible installation (one-click)?

> One for Windows and one for Linux (if possible similar in content)?

Installer for Windows is here:

http://rubyforge.org/frs/?group_id=167

and for Linux:

http://rubyforge.org/frs/?group_id=426

Yours,

Tom


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ilias Lazaridis  
View profile  
 More options Mar 23 2005, 8:58 pm
Newsgroups: comp.lang.ruby
From: Ilias Lazaridis <il...@lazaridis.com>
Date: Thu, 24 Mar 2005 03:58:49 +0200
Local: Wed, Mar 23 2005 8:58 pm
Subject: Re: [EVALUATION] - E03 - jamLang Evaluation Case Applied to Ruby

I will make a slight addition:

The application main code will be alternatively entered in a seperate
file, main.rb:

>   talker = Talker.new
>   talker.sayHello

How does "main.rb" gets knowledge about "talker.rb" ?

-

[...]

>   # string interpolation in action - the bit between the #{} can be
>   # any valid expression; to_s will be called on the result and
>   # interpolated into the string
>   def sayHelloAdvanced
>     puts "#{thisMethodName}: Hello World"
>   end

[...]

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

Can I use another comment marker?

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

.

--
http://lazaridis.com


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jacob Fugal  
View profile  
 More options Mar 24 2005, 1:19 pm
Newsgroups: comp.lang.ruby
From: Jacob Fugal <lukf...@gmail.com>
Date: Fri, 25 Mar 2005 03:19:05 +0900
Local: Thurs, Mar 24 2005 1:19 pm
Subject: Re: [EVALUATION] - E03 - jamLang Evaluation Case Applied to Ruby

Put the code for the Talker class in a separate file then 'require'
that file in main.rb. For example, if the Talker class were in
talker.rb then I'd have:

  require 'talker'

  talker = Talker.new
  talker.sayHello

for my main.rb. Note that the require statement does not need the .rb
suffix on the file containing the class.

> >   # string interpolation in action - the bit between the #{} can be
> >   # any valid expression; to_s will be called on the result and
> >   # interpolated into the string
> >   def sayHelloAdvanced
> >     puts "#{thisMethodName}: Hello World"
> >   end

> "#" 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 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"

The + operator performs concatenation when its arguments are strings.

Jacob Fugal


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Martin Ankerl  
View profile  
 More options Mar 24 2005, 1:30 pm
Newsgroups: comp.lang.ruby
From: Martin Ankerl <martin.ank...@gmail.com>
Date: Thu, 24 Mar 2005 19:30:39 +0100
Local: Thurs, Mar 24 2005 1:30 pm
Subject: Re: [EVALUATION] - E03 - jamLang Evaluation Case Applied to 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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Edward Gray II  
View profile  
 More options Mar 24 2005, 1:33 pm
Newsgroups: comp.lang.ruby
From: James Edward Gray II <ja...@grayproductions.net>
Date: Fri, 25 Mar 2005 03:33:42 +0900
Local: Thurs, Mar 24 2005 1:33 pm
Subject: Re: [EVALUATION] - E03 - jamLang Evaluation Case Applied to Ruby
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.

Ruby has a multiline comment syntax:

=begin
        This is a comment.
=end

Just FYI.

James Edward Gray II


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ilias Lazaridis  
View profile  
 More options Mar 24 2005, 10:57 pm
Newsgroups: comp.lang.ruby
From: Ilias Lazaridis <il...@lazaridis.com>
Date: Fri, 25 Mar 2005 05:57:31 +0200
Local: Thurs, Mar 24 2005 10:57 pm
Subject: Re: [EVALUATION] - E03 - jamLang Evaluation Case Applied to Ruby
Martin DeMello wrote:
> Ilias Lazaridis <il...@lazaridis.com> wrote:

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

> Okay, that's a neat bit of work. Here's a quick solution (ruby makes
> most of this fairly trivial):

> # here's your basic talker class

[...]

preliminary draft:

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

> # reflection
> class Talker

>   # simple

>   def sayYourClassName
>     puts self.class.name # the 'self' is optional here
>   end

omitting "self" (puts class.name) leads to an error

>   # objects know nothing about the variables that are bound to them
>   # (a variable name is not the name of the instance anyway). The
>   # closest you can come to the "name" of an object is it's object id, so...
>   def sayYourInstanceName
>     puts object_id # again, you could say self.object_id if you prefer
>   end

ok

>   # advanced

>   # caller returns a stack (array) of strings of the form
>   #   file:linenumber in `method'
>   # so we extract the most recent one and parse the method name out
>   # code from PLEAC
>   def thisMethodName
>     caller[0] =~ /in `([^']+)'/ ? $1 : '(anonymous)';
>   end

I understand the concept.

is there possibly a more direct solution available, with cleaner code
and a stable/higher execution speed?

>   # string interpolation in action - the bit between the #{} can be
>   # any valid expression; to_s will be called on the result and
>   # interpolated into the string
>   def sayHelloAdvanced
>     puts "#{thisMethodName}: Hello World"
>   end

ok

>   # expert
>   def sayYourClassDefinition
>     puts "Class:"
>     sayYourClassName

     puts "Class #{self.class.name}" >> Class Talker

but

     puts "Class #{sayYourClassName}" >> Talker Class
     puts "Class " + sayYourClassName.to_s >> Talker Class

why?

>     # %{} is another way to write a string literal

#{} - inside strings
%{} - outside of strings

Can I get somehow a more precise reflection of the class definition
(output similar to the original class-def, excluding code)?

>     # note that the instance variables belong to the *instance*, not
>     # to the class, so they're not technically part of the class
>     # definition. the following code is illustrative:
>     #
>     # class A
>     #   attr_accessor :foo, :bar # defines getters and setters
>     # end
>     #
>     # a = A.new
>     # p a.instance_variables # => []
>     # a.foo = 10
>     # p a.instance_variables # => ["@foo"]
>     # b = A.new
>     # p b.instance_variables # => []
>   end

ok

>   def sayYourClassCode
>     puts "Sadly, you cannot introspect the source code from within a program"
>     # though see http://rubyforge.org/projects/parsetree/
>     # for a way to get at the parsed AST
>   end

ok

> end

> # testing it out

> if __FILE__ == $0
>   talker.sayHelloAdvanced
>   talker.sayYourClassName
>   talker.sayYourClassDefinition
>   talker.sayYourClassCode
> end

> Hope this helps.

very much!

thank you for your efforts.

> martin

.

--
http://lazaridis.com


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ilias Lazaridis  
View profile  
 More options Mar 25 2005, 8:26 pm
Newsgroups: comp.lang.ruby
From: Ilias Lazaridis <il...@lazaridis.com>
Date: Sat, 26 Mar 2005 03:26:08 +0200
Local: Fri, Mar 25 2005 8:26 pm
Subject: Re: [EVALUATION] - E03 - jamLang Evaluation Case Applied to Ruby
[from an answer which showed up as a seperate thread]

James Edward Gray II wrote:

> Begin forwarded message:

>>> # reflection
>>> class Talker
>>>   # simple
>>>     def sayYourClassName
>>>     puts self.class.name # the 'self' is optional here
>>>   end

>> omitting "self" (puts class.name) leads to an error

> I checked this one myself, because it surprised me when you said it.
> You're right of course.  I'm assuming it's because class is a method
> name and a Ruby keyword.

ok

I meant something direct like:

caller.active_method

Of course [I've overseen the print statements].

> Then the local puts prints "Class " and the return value
> from the method call, which isn't meaningful in this context.

ok

>>>     # %{} is another way to write a string literal

>> #{} - inside strings
>> %{} - outside of strings

> No, these are not equivalent.  #{...} is for interpolating Ruby code
> inside a string.  %{...} defined a double quoted string, without the
> quotes:

> %{This is a string.  I can use "quotes" in here.  And #{"interpolate"}
> values.}

ok, now I understand.

%{} => string
#{} => string interpolation of code

>>>     # (looks neat for multiline strings)
[...]
>>>     }

>> Can I get somehow a more precise reflection of the class definition
>> (output similar to the original class-def, excluding code)?

> I don't believe so, no.  Remember that a Ruby class can be reopened
> and definitions added to it.  That means a class could be built up
> from many places.

> Ruby does have a means to get it to store the source code it reads,
> but I don't believe that's what you were asking for.

you're right.

-

I will try to work this out myself.

Where can I find the following information?:

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

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

> James Edward Gray II

.

--
http://lazaridis.com


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Florian Gross  
View profile  
 More options Mar 25 2005, 9:05 pm
Newsgroups: comp.lang.ruby
From: Florian Gross <f...@ccan.de>
Date: Sat, 26 Mar 2005 03:05:58 +0100
Local: Fri, Mar 25 2005 9:05 pm
Subject: Re: [EVALUATION] - E03 - jamLang Evaluation Case Applied to Ruby

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)
http://phrogz.net/ProgrammingRuby/ (first issue with Wiki for changes)
http://www.pragmaticprogrammer.com/titles/ruby/index.html (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.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ilias Lazaridis  
View profile  
 More options Mar 25 2005, 11:56 pm
Newsgroups: comp.lang.ruby
From: Ilias Lazaridis <il...@lazaridis.com>
Date: Sat, 26 Mar 2005 06:56:02 +0200
Local: Fri, Mar 25 2005 11:56 pm
Subject: Re: [EVALUATION] - E03 - jamLang Evaluation Case Applied to Ruby

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?

thank you for the information.

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

I cannot locate it.

.

--
http://lazaridis.com


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Florian Gross  
View profile  
 More options Mar 28 2005, 1:07 pm
Newsgroups: comp.lang.ruby
From: Florian Gross <f...@ccan.de>
Date: Mon, 28 Mar 2005 20:07:06 +0200
Local: Mon, Mar 28 2005 1:07 pm
Subject: Re: [EVALUATION] - E03 - jamLang Evaluation Case Applied to Ruby

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.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ilias Lazaridis  
View profile  
 More options Mar 30 2005, 12:02 am
Newsgroups: comp.lang.ruby
From: Ilias Lazaridis <il...@lazaridis.com>
Date: Wed, 30 Mar 2005 08:02:02 +0300
Local: Wed, Mar 30 2005 12:02 am
Subject: Re: [EVALUATION] - E03 - jamLang Evaluation Case Applied to Ruby

Florian Gross wrote:
> Ilias Lazaridis wrote:

>>> I've generated this one automatically:

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

>> with which tool have you generated this?

> Graphviz

http://www.graphviz.org/

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

if it is not very long, can you please post it here?

-

btw:

found this ppt-presentation about the object-model (but would prefere an
UML diagramm):

http://rubyforge.org/docman/view.php/251/96/ChrisPine_UROM.ppt

.

--
http://lazaridis.com


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ilias Lazaridis  
View profile  
 More options Mar 30 2005, 12:26 am
Newsgroups: comp.lang.ruby
From: Ilias Lazaridis <il...@lazaridis.com>
Date: Wed, 30 Mar 2005 08:26:45 +0300
Local: Wed, Mar 30 2005 12:26 am
Subject: Re: [EVALUATION] - E03 - jamLang Evaluation Case Applied to Ruby

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?

.

--
http://lazaridis.com


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Martin Ankerl  
View profile  
 More options Mar 30 2005, 1:31 am
Newsgroups: comp.lang.ruby
From: Martin Ankerl <martin.ank...@gmail.com>
Date: Wed, 30 Mar 2005 08:31:50 +0200
Local: Wed, Mar 30 2005 1:31 am
Subject: Re: [EVALUATION] - E03 - jamLang Evaluation Case Applied to Ruby

> if it is not very long, can you please post it here?

I have a quite similar script here:
http://martinus.geekisp.com/rublog.cgi/Projects/RubyToDot/rubytodot.html

martinus


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Edward Gray II  
View profile  
 More options Mar 30 2005, 9:51 am
Newsgroups: comp.lang.ruby
From: James Edward Gray II <ja...@grayproductions.net>
Date: Wed, 30 Mar 2005 23:51:17 +0900
Local: Wed, Mar 30 2005 9:51 am
Subject: Re: [EVALUATION] - E03 - jamLang Evaluation Case Applied to Ruby
On Mar 29, 2005, at 11:29 PM, Ilias Lazaridis wrote:

> 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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ilias Lazaridis  
View profile  
 More options Mar 31 2005, 4:39 am
Newsgroups: comp.lang.ruby
From: Ilias Lazaridis <il...@lazaridis.com>
Date: Thu, 31 Mar 2005 12:39:51 +0300
Local: Thurs, Mar 31 2005 4:39 am
Subject: Re: [EVALUATION] - E03 - jamLang Evaluation Case Applied to Ruby
James Edward Gray II wrote:

> On Mar 29, 2005, at 11:29 PM, Ilias Lazaridis wrote:

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

ok

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

ok

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

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

> 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

.

--
http://lazaridis.com


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Messages 1 - 25 of 74   Newer >
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google