Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > [EVALUATION] - E03 - jamLang Evaluation Case Applied to Ruby

Reply
Thread Tools

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

 
 
Ilias Lazaridis
Guest
Posts: n/a
 
      03-18-2005
[EVALUATION] - E02 - Nitro, a Ruby Based WebFramework
http://groups-beta.google.com/group/...b8b0824d4fbaec

-

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 With Quote
 
 
 
 
Martin DeMello
Guest
Posts: n/a
 
      03-18-2005
Ilias Lazaridis <(E-Mail Removed)> 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 With Quote
 
 
 
 
Ilias Lazaridis
Guest
Posts: n/a
 
      03-20-2005
Martin DeMello wrote:
> Ilias Lazaridis <(E-Mail Removed)> 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 With Quote
 
Douglas Livingstone
Guest
Posts: n/a
 
      03-21-2005
On Mon, 21 Mar 2005 02:59:53 +0900, Ilias Lazaridis <(E-Mail Removed)> wrote:
> Martin DeMello wrote:
> > Ilias Lazaridis <(E-Mail Removed)> 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.

> > # 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 With Quote
 
Martin DeMello
Guest
Posts: n/a
 
      03-21-2005
Ilias Lazaridis <(E-Mail Removed)> 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 With Quote
 
Ilias Lazaridis
Guest
Posts: n/a
 
      03-21-2005
Douglas Livingstone wrote:
> On Mon, 21 Mar 2005 02:59:53 +0900, Ilias Lazaridis <(E-Mail Removed)> wrote:
>
>>Martin DeMello wrote:
>>
>>>Ilias Lazaridis <(E-Mail Removed)> 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
> 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
 
Reply With Quote
 
Ilias Lazaridis
Guest
Posts: n/a
 
      03-21-2005
Martin DeMello wrote:
> Ilias Lazaridis <(E-Mail Removed)> 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 With Quote
 
Martin Ankerl
Guest
Posts: n/a
 
      03-21-2005
>> 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 With Quote
 
Ilias Lazaridis
Guest
Posts: n/a
 
      03-21-2005
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 With Quote
 
Ilias Lazaridis
Guest
Posts: n/a
 
      03-23-2005
Ilias Lazaridis wrote:
> [EVALUATION] - E02 - Nitro, a Ruby Based WebFramework
> http://groups-beta.google.com/group/...b8b0824d4fbaec
>
> -
>
> 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


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 With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I install Ruby 1.8.4 on my server from source, with all patches applied. Chris Pearl Ruby 0 10-16-2006 12:22 PM
Nikao Dragonfire Case Evaluation Silverstrand Front Page News 0 11-30-2005 01:04 AM
[JAMLANG] - Comparative Evaluation - Draft Version Ilias Lazaridis Perl Misc 2 04-24-2005 06:13 PM
[EVALUATION] - E03 - jamLang Evaluation Case Applied to Python Ilias Lazaridis Python 2 04-24-2005 05:29 PM
[EVALUATION] - E04 - jamPersist Evaluation Case Applied to Ruby Ilias Lazaridis Ruby 18 04-09-2005 04:45 PM



Advertisments