Forum: Ruby basic oO :: First Class Object

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
jansenh (Guest)
on 2006-02-07 08:44
(Received via mailing list)
Hi all.

What is a First Class Object, explained in Ruby context?

regards, Henning
Wilson Bilkovich (Guest)
on 2006-02-07 15:22
(Received via mailing list)
On 2/7/06, jansenh <henning.jansen@gmail.com> wrote:
> Hi all.
>
> What is a First Class Object, explained in Ruby context?
>
> regards, Henning

"first class" is a general programming-language term for something
that has all the features and behaviors of 'richer' parts of the
language.
Usually it's used when pointing out an interesting feature.  Most
languages don't go around crowing about having "first-class Strings".

In Ruby, classes are also objects. Since this is cool and unusual, you
might hear people say that: 'in Ruby, classes are first-class
objects', or 'integers are first-class objects'.
647cdbf36a8159ea5451ed96fd36fde4?d=identicon&s=25 Jules J. (jules)
on 2006-02-07 17:30
I think that something is first-class if you can store it in a variable.
In Ruby, you can store a class in a variable, so it is first-class. So,
a method/function in Ruby is not first class:

a_func = def
  puts 'hi'
end

Does not work. In Javascript, functions are first-class:

a_func = function (){
  alert('hi')
}

You could, offcourse, store a method in a variable with the method()
function in Ruby, but in general, functions are not first-class in Ruby.

Jules
Jacob Fugal (Guest)
on 2006-02-07 17:48
(Received via mailing list)
On 2/7/06, jansenh <henning.jansen@gmail.com> wrote:
> Hi all.
>
> What is a First Class Object, explained in Ruby context?

A good description of First-class objects is here:

  http://en.wikipedia.org/wiki/First-class_object

Note that the above mentions First-class objects, not First-class
Objects. That is, the "object" being talked about is more vague than a
Ruby Object. A First-class object, as defined above, is any entity
(ie. construct) of the language which can be used without restriction
compared to other "objects"/entities in the language.

For Ruby, the prime mechanics of the language are message-passing with
object-references as arguments. Since any entity in the Ruby --
methods[1], integers, classes, strings, IO streams, or anything else
you can think of -- *can* be represented as an Object which can be
passed around and receive messages of it's own, this makes all the
entities in Ruby first-class objects[2].

Jacob Fugal

[1] Method objects are a little special. Specifically, one of the
common criteria for first-class objects is anonymous creation. For
example, and anonymous class can be created with Class.new, and an
anonymous lambda can be created via lambda{}. But while a Method
object can be derived from a method, the native representation of the
method is *not* an object, and the method must exist in a
non-anonymous form before the Method can be obtained. For me, however,
anonymous lambdas make up for this deficiency, especially once the
call semantics become unified and lambdas can handle blocks in 1.9 or
2.0.

[2] As per my definition of the prime mechanics. There may be other
activities within the language for which certain entities are not
first-class, such as mentioned in footnote 1. I also may have missed
something...
jansenh (Guest)
on 2006-02-08 17:59
(Received via mailing list)
Thanx for good explanations! I think I've got the idea now... regards,
Henning
This topic is locked and can not be replied to.