From: Jim Weirich Date: 2006-05-16T06:20:33+09:00 Subject: Re: begining programmer questions Regg wrote: > In your example: > > " In C++ you can do this: > > MyClass * m_class = NULL; > m_class->do_something(); > > And the results are undefined (by the language standard).[1] If you > are > lucky, you might get segmentation fault. > " > This would also generate a error just like it would in Ruby. Ummm ... its undefined. That means it is, ummm, undefined. As in every system might handle it differently. Most modern systems will give you a Segmentation Fault and dump the program. Is there a language defined way to handle that? (I don't recall one ... but my C++ is a bit rusty). In Ruby however, it is a standard exception. You can handle it as easily as handling end of file exceptions or file not found exceptions. > This is something that YOU are forcing the compiler to do. > You are FORCING the compiler to accept this assignment even if it's > wrong. It IS wrong (well, most of the time), and yes, I'm able to force the compiler to accept it anyways (because occasionally it is not wrong). > But in Ruby you are not forcing it, it just allows it to happen with no > warning. That's because it is NOT wrong in Ruby. It's not wrong until you use the object inappropriately, and that that point you get a well defined exception that you can deal with inside the language. C++ attempts to prevent object misuse by making the assignment an error, but letting you override that error and make any misuse your problem. Ruby prevents object misuse by preventing the misuse when it happens, not by objecting to assignments that may or may not be correct. Is this helping? A lot of people coming from statically typed languages deal with this same issue (I was one at one time). We put a lot of faith in the hope that static typing will catch a lot of our errors for us. And it seems that a dynamic language is too "loosey-goosey" to be safe. It turns out (at least for me) the errors that are caught by a static type system and aren't caught by a dynamic system are not all that common. But me telling you these things probably isn't going to convince you one way or another. You need to use the language a while and see if your fears are born out. At any rate, I hope you enjoy some Ruby programming while you are learning! -- Jim Weirich -- Posted via http://www.ruby-forum.com/.