[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Operation on Classes

aartist

5/31/2005 4:28:00 PM

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?

5 Answers

Robert Klemme

5/31/2005 4:35:00 PM

0

aartist wrote:
> 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.rub...

Kind regards

robert

Brian Schröder

5/31/2005 5:47:00 PM

0

On 31/05/05, aartist <aartist@gmail.com> wrote:
> 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?
>
>

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

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

regards,

Brian


--
http://ruby.brian-sch...

Stringed instrument chords: http://chordlist.brian-sch...


aartist

5/31/2005 6:09:00 PM

0

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

Robert Klemme

6/1/2005 8:36:00 AM

0

aartist wrote:
> 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/i...

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

aartist

6/1/2005 2:00:00 PM

0

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.