Advertisement
kn0tsel

Ruby-04

May 6th, 2013
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ~ Differences from C ~
  2.  
  3. Unlike C, in Ruby,…
  4.  
  5. Objects are strongly typed (and variable names themselves have no type at all).
  6.  
  7. There’s no macros or preprocessor. No casts. No pointers (nor pointer arithmetic).    
  8. No typedefs, sizeof, nor enums.
  9.    
  10. There are no header files. You just define your functions (usually referred to as “methods”) and classes in the main source code files.
  11.    
  12. There’s no #define. Just use constants instead.
  13.    
  14. As of Ruby 1.8, code is interpreted at run-time rather than compiled to any sort of machine- or byte-code.
  15.    
  16. All variables live on the heap. Further, you don’t need to free them yourself—the garbage collector takes care of that.
  17.    
  18. Arguments to methods (i.e. functions) are passed by reference, not by value.
  19.    
  20. It’s require 'foo' instead of #include <foo> or #include "foo".
  21.    
  22. You cannot drop down to assembly.
  23.    
  24. There’s no semicolon’s ending lines.
  25.    
  26. You go without parentheses for if and while condition expressions.
  27.    
  28. Parentheses for method (i.e. function) calls are often optional.
  29.    
  30. You don’t usually use braces—just end multi-line constructs (like while loops) with an end keyword.
  31.    
  32. The do keyword is for so-called “blocks”. There’s no “do statement” like in C.
  33.    
  34. The term “block” means something different. It’s for a block of code that you associate with a method call so the method body can call out to the block while it executes.
  35.    
  36. There are no variable declarations. You just assign to new names on-the-fly when you need them.
  37.    
  38. When tested for truth, only false and nil evaluate to a false value. Everything else is true (including 0, 0.0, and "0").
  39.    
  40. There is no char—they are just 1-letter strings.
  41.    
  42. Strings don’t end with a null byte.
  43.    
  44. Array literals go in brackets instead of braces.
  45.    
  46. Arrays just automatically get bigger when you stuff more elements into them.
  47.    
  48. If you add two arrays, you get back a new and bigger array (of course, allocated on the heap) instead of doing pointer arithmetic.
  49.    
  50. More often than not, everything is an expression (that is, things like while statements actually evaluate to an rvalue).
  51.  
  52. ~ Differences from C++ ~
  53.  
  54. Unlike C++, in Ruby,…
  55.  
  56. There’s no explicit references. That is, in Ruby, every variable is just an automatically dereferenced name for some object.
  57.  
  58. Objects are strongly but dynamically typed. The runtime discovers at runtime if that method call actually works.
  59.    
  60. The “constructor” is called initialize instead of the class name.
  61.    
  62. All methods are always virtual.
  63.    
  64. Class” (static) variable names always begin with @@ (as in @@total_widgets).
  65.    
  66. You don’t directly access member variables—all access to public member variables (known in Ruby as attributes) is via methods.
  67.    
  68. It’s self instead of this.
  69.    
  70. Some methods end in a ’?’ or a ’!’. It’s actually part of the method name.
  71.    
  72. There’s no multiple inheritance per se. Though Ruby has “mixins” (i.e. you can “inherit” all instance methods of a module).
  73.    
  74. There are some enforced case-conventions (ex. class names start with a capital letter, variables start with a lowercase letter).
  75.    
  76. Parentheses for method calls are usually optional.
  77.    
  78. You can re-open a class anytime and add more methods.
  79.    
  80. There’s no need of C++ templates (since you can assign any kind of object to a given variable, and types get figured out at runtime anyway). No casting either.
  81.    
  82. Iteration is done a bit differently. In Ruby, you don’t use a separate iterator object (like vector<T>::const_iterator iter) but instead your objects may mixin the Enumerator module and just make a method call like my_obj.each.
  83.    
  84. There’s only two container types: Array and Hash.
  85.    
  86. There’s no type conversions. With Ruby though, you’ll probably find that they aren’t necessary.
  87.    
  88. Multithreading is built-in, but as of Ruby 1.8 they are “green threads” (implemented only within the interpreter) as opposed to native threads.
  89.    
  90. A unit testing lib comes standard with Ruby.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement