Ari Brown wrote: > Hey, > Just a curious question.
> So does ruby have anything to accommodate for it? If not, what about > a work around?
> Thanks, > ~ Ari > English is like a pseudo-random number generator - there are a > bajillion rules to it, but nobody cares.
For your own good, don't do that. Don't work your way around how a language works to simulate some patterns you learned in another language. That just leads to bad code and wasted time (no need to learn a new language if you just continue to code in the other language).
> So does ruby have anything to accommodate for it? If not, what > about a work around?
> Thanks, > ~ Ari > English is like a pseudo-random number generator - there are a > bajillion rules to it, but nobody cares.
Do you mean something like this (example below)? What you should be aware of is that Ruby doesn't require you to cast objects to a particular type in order to call a method. You may have a number of objects of completely different classes in your collection, and as long as they all respond to the method you're interested in then you can iterate through and call that method (duck typing). This makes interfaces redundant and is a fantastically useful feature.
Cheers, Dave
class Animal attr_reader :name
def initialize(name) @name= name end
def noise "some strange grunty sound" end
end
class Dog < Animal def noise "Woof!" end end
class Cat < Animal def noise "Meow" end end
animals= [Dog.new("Fido"), Cat.new("Socks"), Animal.new("Suzi")] animals.each do |animal| puts "#{animal.name} says #{animal.noise}" end
On 7/8/07, Stefan Rusterholz <apei...@gmx.net> wrote:
> For your own good, don't do that. Don't work your way around how a > language works to simulate some patterns you learned in another > language. That just leads to bad code and wasted time (no need to learn > a new language if you just continue to code in the other language).
Stefan, thanks for defending the ducks ;). But I feel that you forget that Ruby is perfectly polymorphic as Sharon has shown above. I do not really see how DT and Polymurphy ;) are related.
Stefan Rusterholz wrote: > Ari Brown wrote: >> Hey, >> Just a curious question.
>> So does ruby have anything to accommodate for it? If not, what about >> a work around?
> For your own good, don't do that. Don't work your way around how a > language works to simulate some patterns you learned in another > language. That just leads to bad code and wasted time (no need to learn > a new language if you just continue to code in the other language).
The difference between Polymorphism and Dynamic-Typing is essentially that the former is done at compile-time and the latter at runtime. The similarity between them; however, is that they more-or-less do the same thing.
So, to be technical, Ruby is _not_ a Polymorphic language. That being said, Dynamic Typing make Ruby act Polymorphic.
-- ************************************* * Travis D Warlick, Jr * Lead Developer * Operis Systems, LLC *************************************
On 7/8/07, Travis D Warlick Jr <warli...@operissystems.com> wrote:
> The difference between Polymorphism and Dynamic-Typing is essentially > that the former is done at compile-time and the latter at runtime. The > similarity between them; however, is that they more-or-less do the same > thing.
In that context Stefan's response would indeed make some sense, I do however not adhere to the differentiation. Polymorphic behavior seems completely unrelated to implementation, it is IMHO a dangerous path to walk, to define a language by it's implementation details.
> So, to be technical, Ruby is _not_ a Polymorphic language. That being > said, Dynamic Typing make Ruby act Polymorphic.
Robert -- I always knew that one day Smalltalk would replace Java. I just didn't know it would be called Ruby -- Kent Beck
Seems I have to clear things a bit up, as I got the feeling I'm misunderstood. I don't say ruby doesn't have X or Y or so. I say asking "How do I do <Pattern A known from language X> in <language Y>" is the wrong approach. That way you end up asking (contrieved example ahead) how to do a for loop in ruby and in turn iterate over e.g. an array using some odd construct intended to simulate a for loop which doesn't exist 1:1 in ruby instead of just using the way nicer each. Instead IMHO you should ask "How do I solve problem X?" As in "how do I iterate over an array?"
On Jul 7, 2007, at 11:21 PM, Sharon Phillips wrote:
> Do you mean something like this (example below)? > What you should be aware of is that Ruby doesn't require you to > cast objects to a particular type in order to call a method. You > may have a number of objects of completely different classes in > your collection, and as long as they all respond to the method > you're interested in then you can iterate through and call that > method (duck typing). This makes interfaces redundant and is a > fantastically useful feature.
<snip>
Not quite. What I mean is is there a way to make Ruby actually modify the code?
~ Ari English is like a pseudo-random number generator - there are a bajillion rules to it, but nobody cares.
I also know that various people have looked into Ruby code obfuscation; try Googling for "ruby obfuscation". I don't think there's any language feature that's specifically intended to support the idea, though.