Ruby Talk Slides
Download
Report
Transcript Ruby Talk Slides
Ruby from a Newbie
Ravi Shankar
Newbie Developer,
Amazon India Development Centre,
Chennai
Statutory Warning
You’re learning from a complete newbie who’s
determined to make a fool of himself
What are we looking at?
A little History behind Ruby
Basics of Ruby
Recommended Books
Questions
History
One upon a time in South Japan
Yukihiro “Matz” Matsumoto
Developed in 95
Current version is 1.8.5
But first
Ruby Interpreter
Interactive Ruby Shell (irb)
Ruby Interactive (ri)
Hello, World!
Ruby who?
Completely OO
*Everything* is an object
Duck Typing and the likes
The duck test :
“When I see a bird that walks like a
duck, swims like a duck and quacks
like a duck, I call that bird a duck”
Because it “ducks” the issue of typing
Ruby is Dynamically and Strongly
typed
Attributes and methods can be added
at run time
Data Structures
Numbers
Fixnum and Bignum – Transparently
Changed
Strings
Single Quote and Double Quote
Comparable
Slicable
Array
Hash
Ranges
Expressions and Operators
Every expression has a value
Extra operator <=>
Short Circuited Boolean operators
and binds lower than &&
or binds lower than ||
For blocks and functions, the value of
the last statement it’s value
=== operator. We needn’t worry now
Conditional statements and
loops
Conditional, if and unless
Looping while, until and for
Inline expressions
Case expressions
Two forms
Uses === operator for comparison
Functions
def foo
puts “What’s up doc?”
1
end
Closures
The way to iterate in Ruby
Closure - Anonymous blocks of code
Variables declared in the callee are
accessible to the called blocks
Used in iterating
Each Object declared iterators
accordingly
So, you want your own
iterators eh?
yield to be empowered!
Remember :you can even add
iterators (for that matter anything!) to
existing classes
Regular Expressions
Built-in Support
/reg-ex/ or %r{reg-ex}
Used as ranges as triggers
=~ and !~ operators
$`, $& and $’
Classes in Rubyland
Constructor named initialize
puts, gets are defined in Kernel
Change Objects at runtime
Operator overloading
Garbage Collection
Methods are public by default
Attributes are private by default
What’s in a name?
local_variable
ConstantName
:symbol_name #Don’t worry about this
@instance_variable
@@class_variable
$global
ClassName ModuleName
method_name
Accessors
Modules and Mixins
Modules are used as namespaces
Modules can be used as mixins
Mixins make multiple inheritance
possible
Exception Handling
begin
#So something
rescue ExceptionType
#hande it and call
#raise to rethrow is needed
ensure
#like finally in java
end
Things I haven’t covered
Only nil and false evaluate to false
#{}
File IO – straightforward
retry, continue and redo in exceptions
Backtick operator
Global $ variables
Books that you can read
The pickaxe book – “Programming
Ruby, The pragmatic programmer’s
guide” will be all you need.
The first edition is freely available
http://www.ruby-lang.org
Why’s poignant guide to Ruby – an
amusing read
Questions?
Thank you!