Ruby Object Model Contd

We will look at “module” objects. “module” objects are instances of “class Module” which inherits from “class Object”. Like “class” objects “module” objects can be created in two ways module Samba CONST = 1 def Samba.module_method end def instance_method end end This is what happening at the time of execution Samba = Module.new Samba.module_eval do def module_method end end Samba.instance_eval do def instance_method end end “module” objects can be used in two ways. 1. “class” objects can “include” “module” objects, then module object become immediate ancestor to “class” objects. That means “module” object is inserted between “class” and “Object” in hierarchy. 2. Any object can “extend” module. That means all the consts and instace methods of module are available to object


Advertisements

C/C++ String constants

C/C++ String constants are very unique. They will be allocated in global read only memory. One string literal will be allocated only once if we use thousand times also. For example char *a = “samba”; is a string constant, but not these char a[] = “samba”; string a = “samba”; The following are the advantages of string constants 1. We can store only pointer in our datastructure (no need of strdup) for example struct A { int x; char *y; }; A a = {20, “samba”}; 2. We can use pointer comparison than string comparison for example char *a[] = {“samba”, “siva”, “rao”}; for(int i = 0 ; i < sizeof(a)/ sizeof(char*) ; ++i) { if( a[i] == “samba”) // here is pointer comparison //do something } So when there is need for readonly string data, please use string literal


Conditional expressions

In conditional expressions, we may write ‘=’ instead of ‘==’, but compiler won’t complain. We can use compiler to catch such errors by following this tip “Reverse right hand side of the expression with left hand side of the expression” Examples: 1. a = 0 is not error but 0 = a is error 2. a = b + c is not error but b + c = a is error Only case where this tip won’t work is a = b

To find the length of string litteral or char array, We can use sizeof than strlen. sizeof is evaluated by the compiler where as strlen is evaluated at runtime.


Examples 1. int len = sizeof(“akamai”) – 1; 2. Char a[] = “akamai”; int len = sizeof(a) – 1; 3. Char *a = “akamai”; don’t use sizeof, please use strlen(a);

Keeping formatting information and using RCS Keywords

When two or more people working on the same file, We may use different editors or even different settings within same editor We can keep some formatting information within source files, which would be understood by editors like vim/emacs For example /* -*- mode:C++; c-basic-offset:2; indent-tabs-mode:nil -*- */ (For Emacs) /* vim: ts=4:sw=4:cindent */ (For Vim) We can add some more settings or change the above settings Using RCS KeyWords:
If we keep $Id:$ in source files, most of the SCMs puts filename with the full version info inside of it. For example  
// $Id: $ Or 
if we want to use the version information in our code static char fileVersion[] = “$Id:$


Here is the link about RCS Keywords kb.perforce.com/?article=054

Ruby Object Model

In Ruby Everything is object. We will see “class” objects. “class” objects are instance of “class Class” which inherits from “class Module” which inherits from “class Object” which mixins with “module Kernel” class objects can be created in two ways Conventional way class Samba attr_accessor :a, :b def Samba.classmethod end def instancemethod end end Actually this is what happening at the time of execution Samba = Class.new Samba.attr_accessor :a, :b Samba.class_eval do def classmethod end end Samba.instance_eval do def instance_method end end As Samba is instance of “class Class”, all the public, protected “instance members” of Class, Module, Object, Kernel are available to object Samba Samba is also class, which implicitly inherits from “class Object” x = Samba.new Now x has access to public and protected instance member of Object and Kernel