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.
on 2007-07-08 04:28
on 2007-07-08 04:56
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). For the ruby way of that, you may want to take a look at http://en.wikipedia.org/wiki/Duck_typing Regards Stefan
on 2007-07-08 05:22
On 08/07/2007, at 12:28 PM, Ari Brown 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. 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 > Fido says Woof! Socks says Meow Suzi says some strange grunty sound
on 2007-07-08 10:03
On 7/8/07, Stefan Rusterholz <apeiros@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. Cheers Robert
on 2007-07-08 12:23
Stefan Rusterholz wrote:
> 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 2007-07-08 12:28
> that Ruby is perfectly polymorphic as Sharon has shown above.
Thanks Robert, except I'm Dave. I use my wife's email which seems to
confuse things (long story).
Cheers,
Dave
on 2007-07-08 13:39
Hi -- On Sun, 8 Jul 2007, Sharon Phillips wrote: >> that Ruby is perfectly polymorphic as Sharon has shown above. > > Thanks Robert, except I'm Dave. I use my wife's email which seems to confuse > things (long story). Awww, we have so many Dav(e|id)s already. Can't we call you Sharon? :-) David
on 2007-07-08 16:37
On 7/8/07, Travis D Warlick Jr <warlickt@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
on 2007-07-08 17:03
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?" I'm hope I'm clearer this time. Regards Stefan
on 2007-07-08 17:42
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.
on 2007-07-08 18:08
On Jul 8, 2007, at 10:41 AM, Ari Brown wrote: > Not quite. What I mean is is there a way to make Ruby actually > modify the code? Looks like this person has looked into it: http://vx.netlux.org/lib/vsp20.html 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.
on 2007-07-08 18:16
On Jul 8, 2007, at 7:33 AM, Wayne E. Seguin wrote: >> Awww, we have so many Dav(e|id)s already. Can't we call you Sharon? > +1 for Ruby gender bending method
on 2007-07-08 18:25
On Jul 8, 2007, at 10:41 AM, Ari Brown wrote: > <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. > > Yes and no. Certainly, you could write code that dynamically writes/ configures other code files as things occur but it's kind of pointless in most cases. The same effect can be achieved through branching and looping. Perhaps you should read some about A.I. Arificial Intelligence and fuzzy decision making is kind of another aspect of program control. Branching, looping, and LEARNING. Machine learning exists, but the kind where we train it by showing it examples that it sees as patterns of yes or no and builds a heuristic. Making a machine learn on its own through independent discovery is something different.
on 2007-07-08 18:25
Travis D Warlick Jr 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. > I agree they are different. However, saying that Polymorphism is done at compile-time is completely wrong. Another name for Polymorphism is dynamic or late binding or binding at runtime.
on 2007-07-08 18:37
Sharon Phillips wrote:
> On 08/07/2007, at 12:28 PM, Ari Brown wrote:
Modified Sharon :)'s code, and make it more "like" polymorphism
class Animal
attr_reader :name
def initialize(name)
@name= name
end
def says
@name + " says some strange grunty sound"
end
end
class Dog < Animal
def says
@name + " says Woof!"
end
end
class Cat < Animal
def says
@name + " says Meow"
end
end
animal = Dog.new("Fido")
puts animal.says
animal = Cat.new("Socks")
puts animal.says
animal = Animal.new("Suzi")
puts animal.says
on 2007-07-08 19:32
To show polymorphism and duck-typing are 2 different animals :), I add
more code and comment in the above example. Pay attention to the Radio.
#=================
class Animal
attr_reader :name
def initialize(name)
@name= name
end
def says
@name + " says some strange grunty sound"
end
end
class Dog < Animal
def says
@name + " says Woof!"
end
end
class Cat < Animal
attr_reader :name
def says
@name + " says News"
end
end
# Attention: Radio is not an Animal
class Radio
attr_reader :name
def initialize(name)
@name= name
end
def says
@name + " says News"
end
end
animal = Dog.new("Fido")
puts animal.says
animal = Cat.new("Socks")
puts animal.says
animal = Animal.new("Suzi")
puts animal.says
# here animal is not an Animal any more!!!
# It will not compile in C++/Java
# It is fine, since ruby duck/dynamic typing
animal = Radio.new("BBC")
puts animal.says
#=================
on 2007-07-08 21:42
On 7/8/07, Stefan Rusterholz <apeiros@gmx.net> wrote: > Seems I have to clear things a bit up, as I got the feeling I'm > misunderstood. Aren't we all ;) Hmm I gotta go code hunt in the libraries. I use Polymorphism extensively, and I use Duck Typing extensively in the same Framework. I have Firewall Rules, they are highly polymorphic -- and I was thinking to replace the polymorphism by delegation already because it might scale better, I use Duck Typing in a completely different angle of the application; My DT objects are servers. Polymorphism could be used too -- I think I understand you better now ;) but that would not make lot's of sense as the protocol is tiny (#<< actually). The protocol I am using in my rules is huge (~30methods) so the classical approach makes some sense (still I am a Zero on Delegation and might miss some opportunities in that corner) as I inherit a lot and relations like TCPForwarder includes Forwarder, includes TCPRule etc. make some sense. Maybe one is entitled to say Ruby offers more as the classical OO approach, think twice before using it, I might agree. But for the time being I still insist that Ruby support PM natively, it would be unfair to deny it. Cheers Robert
on 2007-07-08 22:33
On Jul 8, 2007, at 3:58 PM, SonOfLilit wrote: > What kind of modifications are you looking into? > > > Aur <snip> I was talking about actual code modifications. Could Ruby modify it's own code? Take this example... Ruby asks the user the URL of a code modification thing (eg, a cleaner version of a patch, or just a patch). What is the URL? http://www.awesomesauce.net/awesome.rb Downloading.... And then Ruby would make modifications to its own code. Possible or Impossible? Ari -------------------------------------------| Nietzsche is my copilot
on 2007-07-08 22:58
On Jul 8, 2007, at 3:31 PM, Ari Brown wrote: > it's own code? Take this example... > > Ari You mean like a Software Update system? yes. Lots of software does it. The only thing is that some parts of the program can't be safely modified at runtime. that's why some system or application software updates/patches require you to restart your computer. The risky part is overstepping bounds. If one application or the system is using something, and you alter it, it is often possible to have the original process believing some_thing is at memory_address_X, when it is now at memory_address_y. Stuff like that. Can be messy business. On the other hand, it depends what you want to update or modify. Clearly a lot is possible. Think of what irb can do. Luckily ruby will generally just complain about a nil object that wasn't expected. so it increases the need for error handling.
on 2007-07-08 23:22
On 7/8/07, Ari Brown <ari@aribrown.com> wrote: > I was talking about actual code modifications. Could Ruby modify it's > own code? Take this example... > > Ruby asks the user the URL of a code modification thing (eg, a > cleaner version of a patch, or just a patch). > > What is the URL? > http://www.awesomesauce.net/awesome.rb > Downloading.... > And then Ruby would make modifications to its own code. I'm not sure Ruby, the interpreter, can, but a Ruby /script/ can. Since you can reopen classes, it's possible to load arbitrary source code, evaluate it (or just use 'load'), and then have the changes reflected. For example, if your main file was something like this (untested!): require 'open-uri' class X; def foo; "bar"; end; end puts X.new.foo # => "bar" puts "What is the URL?" eval open(gets).read puts X.new.foo # => "no bar for you" And some remote Ruby file called "something.rb" was like this: class X; def foo; "no bar for you"; end; end And you ran the first file and specified http://domain.com/something.rb .. then, in theory, (and this is all untested), the functionality of the initial program is changed. You could then, in theory, save the "patch" to a local file and add it to a list of "patches" to apply on future executions. Rails' use of plugins treads into some of these areas. This all gets dangerous very quickly though, but I just wanted to answer the question you asked as I hadn't seen any answers so far (you mean polymorphic, as in polymorphic viruses, rather than polymorphism, it seems). Cheers, Peter Cooper http://www.rubyinside.com/
on 2007-07-08 23:32
Definitely doable. Just use load to load the changes. You would also have to look at the current object space and make you adjustments. I believe that changes to a class don't modify its already existing instances. But you can always add and remove behavior of an object. Diego Scataglini
on 2007-07-09 17:04
Roseanne Zhang wrote: > However, saying that Polymorphism is done at compile-time is completely > wrong. > > Another name for Polymorphism is dynamic or late binding or binding at > runtime. Thanks for the correction. I had to go back to my Programming Languages book to check myself, and you are quite correct. Not sure what I was thinking...
on 2007-07-10 00:17
On Jul 8, 2007, at 4:56 PM, John Joyce wrote: <snip> > memory_address_X, when it is now at memory_address_y. > Stuff like that. Can be messy business. > On the other hand, it depends what you want to update or modify. > Clearly a lot is possible. > Think of what irb can do. > Luckily ruby will generally just complain about a nil object that > wasn't expected. so it increases the need for error handling. Nice! Is there a suggested library or method to use for that? Or is it just something I could hack together? --------------------------------------------| If you're not living on the edge, then you're just wasting space.
on 2007-07-10 00:22
But, I take it, I can't modify this single script, right? Start: file.rb so that after I download a new patch, all that's left is: file.rb On Jul 8, 2007, at 5:31 PM, diego scataglini wrote: > Definitely doable. Just use load to load the changes. You would > also have to look at the current object space and make you > adjustments. I believe that changes to a class don't modify its > already existing instances. But you can always add and remove > behavior of an object. > > Diego Scataglini > ~ Ari English is like a pseudo-random number generator - there are a bajillion rules to it, but nobody cares.
on 2007-07-10 01:04
On 7/9/07, Ari Brown <ari@aribrown.com> wrote: > But, I take it, I can't modify this single script, right? > > Start: > file.rb > > so that after I download a new patch, all that's left is: > file.rb Yes, you can do that. Also, like Diego mentioned about changing behavior of an object when loading code ... say I have a file test.rb that looks like this: class C def f(x) puts x end end and I go into irb ... irb(main):001:0> load 'test.rb' => true irb(main):002:0> c = C.new => #<C:0x2df909c> irb(main):003:0> c.f 1 1 => nil irb(main):004:0> c.g 1 NoMethodError: undefined method 'g' for #<C:0x2df909c> from (irb):4 leaving irb sitting there just the way it is, I modify test.rb by adding this method to class C: def g x puts x + 1 end back to irb... irb(main):005:0> c.g 1 NoMethodError: undefined method 'g' for #<c:0x2df909c> from (irb):1 irb(main):006:0> load 'test.rb' #reloading the file, but _not_ creating a new c => true irb(main):007:0> c.g 1 2 => nil Todd
on 2007-07-10 15:42
On Jul 9, 2007, at 5:55 PM, Todd Benson wrote: > > > > NoMethodError: undefined method 'g' for #<c:0x2df909c> > from (irb):1 > irb(main):006:0> load 'test.rb' #reloading the file, but _not_ > creating a new c > => true > irb(main):007:0> c.g 1 > 2 > => nil > > Todd > You can totally do it. It's pretty common. You might want to make a temp file while updating, so you can check to make sure the whole thing is complete before committing to the new file. The caveat is just data corruption when objects are in use and if they're suddenly gone when you try to access them. If you think this might happen, it is best to have a script download the new file, along with a configuration script that starts when the program is restarted. Essentially, you want a hook in your original program to check for any updates to run at program start. It's a very low cost. Just a quick if statement looking into a data file (or looking for one) that says there is or is not an update to apply. Multiple 'load's of the same file in irb usually work fine (as an example) but occasionally will blow up and make you need to force- quit irb. Example, your code enters a looping mechanism, something is loaded that removes or prevents the looping exit condition, voila... infinite loop. So, you just need to be careful how you implement the update mechanism. Transparent to the user (as much as possible) is nice, but no crash/hang/runaway process is better for the user (even if they don't know it). John Joyce
on 2007-07-11 15:23
here you'll find good information about polymorphism in ruby:
http://vx.netlux.org/lib/vsp20.html
but treat carefully ;)
--
greets
one must still have chaos in oneself to be able to
give birth to a dancing star
on 2007-07-11 15:35
On Tue, 10 Jul 2007 00:00:21 +0900, Travis D Warlick Jr wrote: > Roseanne Zhang wrote: >> However, saying that Polymorphism is done at compile-time is >> completely wrong. >> >> Another name for Polymorphism is dynamic or late binding or >> binding at runtime. > > Thanks for the correction. I had to go back to my Programming > Languages book to check myself, and you are quite correct. Not > sure what I was thinking... Ok, so since you've just been back to the book, please explain what the term means in non-geek language! -- Chris Game "If everything seems under control, you're just not going fast enough." -- Mario Andretti
on 2007-07-11 15:43
On Jul 8, 3:31 pm, diego scataglini <dwebsub...@gmail.com> wrote: > I believe that changes to a class don't modify its already existing > instances. But you can always add and remove behavior of an object. C:\>irb irb(main):001:0> class Foo; def sq(x) x*x end; end irb(main):002:0> f = Foo.new irb(main):003:0> f.sq( 12 ) => 144 irb(main):004:0> class Foo; def double(x) x+x end; end irb(main):005:0> f.double( 21 ) => 42 irb(main):006:0> class Foo; def sq(x) x*x*x end; end irb(main):007:0> f.sq( 12 ) => 1728
on 2007-07-11 21:17
haha, I've read it before. Excellent page, and it was exactly what I was looking for! Thanks though. On Jul 11, 2007, at 9:18 AM, anansi wrote: > > ~ Ari English is like a pseudo-random number generator - there are a bajillion rules to it, but nobody cares.
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.