Ruby On Rails Learning Ruby Through Examples
Ruby On Rails Learning Ruby Through Examples
Ruby On Rails Learning Ruby Through Examples
Some useful links that come handy when you are going through the course material:
Ruby Download
- http://www.ruby-lang.org/en/downloads/
Ruby Online
- http://tryruby.hobix.com/
Duck Typing
- http://en.wikipedia.org/wiki/Duck_typing
- http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/100511
Duck typing:
Duck typing is way of thinking about programming in Ruby.
For folks who come from languages such as Java or C++, types are
basically the same as classes. When you ask 'what is the type of
"cat"?', the answer comes back as 'String'.
These languages use the type==class model as the model for programming.
You say
String fred;
to say that 'fred' has type 'String', and the language implements that
by saying that fred can only reference objects that are class String.
Ruby doesn't use that model. In Ruby, types are defined as the
capabilities of objects. Classes can be used to give objects their
initial capabilities, but from that point on, the class is (almost)
irrelevant. When I write
We here pick up basics of Ruby just enough to get going with Rails and start developing simple
websites and web applications.
All the things that we had said about ruby in earlier post What is Ruby? will be discussed in
coming posts.
Before even going for installing the ruby in your desktop, let us experiment on ruby with small
examples and work/execute them here online.
Coming on to the variable declaration, ruby does not require the variable declarations. Let us the
see this point also:
you type
>> a=5
=>5
>>b=2.0
=>2.0
>>c=a+b
=>7.0
As you see we have not declared the variables a,b,c and what type they hold. All these variables
are evaluated at the run time and treated accordingly.
Ruby treat the operators(+,-,*,/,%...) as just like methods, which is conceptually derived from
Smalltalk.
Examples:
#Addition
>> 3+2
=> 5
#Subtraction
>> 5-1
=> 4
#Multiplication
>> 5*2
=> 10
#Division
>> 10/2
=> 5
#Modulus Division
>> 10%2
=> 0
Ruby needs no variable declarations.
# Variable declaration. No type is mentioned.
>> i=5
=> 5
>> k=9
=> 9
# Adding two variables
>> i+k
=> 14
# Adding two variables and assigned to another variable
>> c=i+k
=> 14
>> c
=> 14
# Float assigned to “a”
>> a=5.0
=> 5.0
# Integer assigned to “b”
>> b=2
=> 2
# The result of adding them is float.
>> a+b
=> 7.0
Discuss/Comment @ TechSavvy