Operation on Classes

A

aartist

While learning ruby, I want to study from the perspective of classes.

Exmaple:
Given: # Array, Index, Object
Operations: array[index] = obj -> obj

Given: # Two Arrays
array & other_array -> an_array
array + other_array -> an_array
array - other_array -> an_array
array <=> other_array -> -1, 0, +1
array == other_array -> bool

Given: # Array, Object
array.push(obj) -> array
array << obj -> array

etc..

So the idea is to know, what is possible with given objects of
different kind.

Is there any way to generate it?
 
R

Robert Klemme

aartist said:
While learning ruby, I want to study from the perspective of classes.

Exmaple:
Given: # Array, Index, Object
Operations: array[index] = obj -> obj

Given: # Two Arrays
array & other_array -> an_array
array + other_array -> an_array
array - other_array -> an_array
array <=> other_array -> -1, 0, +1
array == other_array -> bool

Given: # Array, Object
array.push(obj) -> array
array << obj -> array

etc..

So the idea is to know, what is possible with given objects of
different kind.

Is there any way to generate it?

There might be but I'd say it's not really worthwhile. The reason being
that method signature and behavior can change at any time because anybody
can override / change these methods at runtime.

Why not just stick with the documentation?
http://www.ruby-doc.org/

Kind regards

robert
 
B

Brian Schröder

While learning ruby, I want to study from the perspective of classes.
=20
Exmaple:
Given: # Array, Index, Object
Operations: array[index] =3D obj -> obj
=20
Given: # Two Arrays
array & other_array -> an_array
array + other_array -> an_array
array - other_array -> an_array
array <=3D> other_array -> -1, 0, +1
array =3D=3D other_array -> bool
=20
Given: # Array, Object
array.push(obj) -> array
array << obj -> array
=20
etc..
=20
So the idea is to know, what is possible with given objects of
different kind.
=20
Is there any way to generate it?
=20
=20

Maybe I do not understand you correctly, but every object has the
class function.

$ irb
irb(main):001:0> (Array.new + Array.new).class
=3D> Array
irb(main):002:0> ([] & []).class
=3D> Array
irb(main):003:0> ([1,2,3] <=3D> [])
=3D> 1
irb(main):004:0> ([1,2,3] <=3D> []).class
=3D> Fixnum
irb(main):005:0> ([1,2,3] =3D=3D []).class
=3D> FalseClass

regards,

Brian


--=20
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/
 
A

aartist

The main idea is about creating templates. In life and in general,
when you encounter, existing set of conditions, you are most likely to
do a solution that you already know for those conditions. Consider
conditions as set of items and you should know the operations that you
can perform with given set of items.

Here, my attept is to document all templates, possible for the basic
classes that Ruby Provides. I can convert the given problem into set of
known items, and then I can apply template that can match. So it
become very easy to know what is possible. This can reduce lots of
mental efforts, while programming..
 
R

Robert Klemme

aartist said:
The main idea is about creating templates. In life and in general,
when you encounter, existing set of conditions, you are most likely to
do a solution that you already know for those conditions. Consider
conditions as set of items and you should know the operations that you
can perform with given set of items.

Here, my attept is to document all templates, possible for the basic
classes that Ruby Provides. I can convert the given problem into set
of known items, and then I can apply template that can match. So it
become very easy to know what is possible. This can reduce lots of
mental efforts, while programming..

If I understand you correctly you kind of want to introduce static typing
through the backdoor. There has been a lot of debate about this, whether
static typing should be present in Ruby and how it can be done. You'll
find it in the archives:
http://blade.nagaokaut.ac.jp/ruby/ruby-talk/index.shtml

Apart from the static typing issue: this sounds like a brute force
approach to development that I still fail to see the benefits of. At
least it seems to me the effort is much too high vs. the benefits gained -
if it works at all. Do you know any success stories where this approach
has been applied?

Also, determining input and output types doesn't tell you the semantics of
a method. You need more detailed information about the properties of the
pre and after states.

Kind regards

robert
 
A

aartist

Well, all I am saying is that I can group bunch of things together to
understand basics classes.

Example #Array, ItemBlock

array.each {|item| block } -> array
array.reverse_each {|item| block }
array.delete_if {|item| block } -> array

array.select {|item| block } -> an_array
array.collect {|item| block } -> an_array
array.reject {|item| block } -> an_array
array.map {|item| block } -> an_array

array.map! {|item| block } -> array
array.collect! {|item| block } -> array
array.reject! {|item| block } -> array or nil


==================================
#Array, IndexBlock
array.each_index {|index| block } -> array
array.fill {|index| block } -> array

The effort can be used towards mastering ruby. Even you may be able to
read the programs faster.
 

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

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,478
Latest member
ReginaldVi

Latest Threads

Top