Saturday, July 01, 2006

Ruby FAQ



What do you mean by "Ruby is a Dynamic programming language".
In computer science, a dynamic programming language is a kind of programming language in which programs can change their structure as they run: functions may be introduced or removed, new classes of objects may be created, new modules may appear. Refer here for more details.
Can we delete an entry in the middle and shift the remaining entries, in an array?
The delete method of an array can do that, for example:
a = ["a", "b", "b", "c"]
a.delete("b")

The contents of array are now "a" and "c".
Does Ruby have associative arrays like awk?
Hashes (sometimes known as associative arrays, maps or dictionaries) are available in Ruby.
How is memory managed for Strings in Ruby? Is there a separate pool for Strings?
Strings are objects of class String. The String class has more than 75 standard methods. If you refer to Ruby User's Guide, it says that "we do not have to consider the space occupied by a string. We are free from all memory management."
Does Ruby allow us to write functions that can accept variable number of parameters?
Yes. See this example:
def print_nums(*numbers)

end
Can we have an anonymous class in Ruby?
Yes. Refer page 382 of the second edition of the book Programming Ruby, by Dave Thomas
Does Ruby have Multiple Inheritance?
Ruby uses modules to implement mix-ins that simulate multiple inheritance.
Does Ruby allow Object Serialization?
Java features the ability to serialize objects, letting you store them somewhere and reconstitute them when needed. Ruby calls this kind of serialization marshaling. Saving an object and some or all of its components is done using the method Marshal.dump. Later on you can reconstitute the object using Marshal.load. Ruby uses marshaling to store session data.
Java and Ruby Similarities:
As with Java, in Ruby...
  • memory is managed for you via a garbage collector.
  • there’s public, private, and protected methods.
  • you’ve got embedded doc tools (Ruby’s is called RDoc). The docs generated by rdoc look very similar to those generated by javadoc. RDoc can produce fairly good content even if the source contains no comments.
Java and Ruby Differences:
Unlike Java, in Ruby...
  • you don’t need to compile your code. You just run it directly.
  • there’s different GUI toolkits. Ruby users can try WxRuby,
    FXRuby, or the bundled-in Ruby Tk for example.
  • you use the end keyword after defining things like classes, instead of having to put braces around blocks of code.
  • you have require instead of import.
  • all member variables are private. From the outside, you access everything via methods.
  • parentheses in method calls are usually optional and often omitted.
  • everything is an object, including numbers like 2 and 3.14159. Classes are objects! For example, Array is a constant name that is bound to the Array class object. To create a new object, we call new on the class object as in a = Array.new
  • there are no primitives or data types
  • variable names are just labels (not objects). They don’t have a type associated with them.
  • there’s no type declarations. You just assign to new variable names as-needed and they just “spring up” (i.e. a = [1,2,3] rather than int[] a = {1,2,3};).
  • it’s foo = Foo.new( "hi") instead of foo = new Foo( "hi" ).
  • the constructor is always named “initialize” instead of the name of the class.
  • you have “mixin’s” instead of interfaces.
  • YAML tends to be favored over XML.
  • it’s nil instead of null. Also, nil is a normal object; you can never get a null pointer error!
  • there is no method overloading.
  • it's much more common to put many classes in the same file.

This is a work-in-progress.

First Post | Previous | Next


Technorati Tags:
Blogs linking to this article

1 Comments:

Blogger Aspirations said...

Just realized that when configuring database.yml file,

development:
adapter: mysql
database: student
username: root
password: cybage@123
host: localhost

There has to be one space before the values. e.g.
password:cybage@123 won't work.

Anish

12:42 PM  

Post a Comment

<< Home