Introduction to Ruby

1,209 views

Published on

Published in: Technology, Education
1 Comment
1 Like
Statistics
Notes
  • Thank you for creating this slide, I've learned something today.
    http://www.gardeningmania.net/
    http://www.gardeningmania.net/category/nutrients-plants/
    http://www.gardeningmania.net/category/long-lasting-perennials/
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
1,209
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
25
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide

Introduction to Ruby

  1. 1. Introduction to Ruby
  2. 2. Ruby is... ● A dynamic ● open source ● focus on simplicity and productivity ● elegant syntax
  3. 3. Download & Install Now Windows ● http://rubyforge.org/frs/download.php/47082/ruby186-2 Linux ● sudo apt-get install ruby irb rdoc
  4. 4. ● Interactive Ruby ● Methods ● Classes ● Objects ● Blocks and Looping ● From PHP to Ruby
  5. 5. Interactive Ruby irb(main):001:0> irb(main):001:0> "Hello World" => "Hello World" irb(main):002:0> puts "Hello World" Hello World => nil
  6. 6. Module irb(main):004:0> 3*2 => 6 irb(main):005:0> 3**2 => 9 irb(main):006:0> Math.sqrt(9) => 3.0 irb(main):007:0> a = 3 ** 2 => 9 irb(main):008:0> b = 4 ** 2 => 16 irb(main):009:0> Math.sqrt(a+b) => 5.0
  7. 7. Methods irb(main):010:0> def h irb(main):011:1> puts "Hello World!" irb(main):012:1> end => nil #Run the method irb(main):013:0> h Hello World! => nil irb(main):014:0> h() Hello World! => nil
  8. 8. Methods irb(main):015:0> def h(name) irb(main):016:1> puts "Hello #{name}!" irb(main):017:1> end => nil irb(main):018:0> h("Jins") Hello Jins! => nil irb(main):019:0> def h(name = "World") irb(main):020:1> puts "Hello #{name.capitalize}!" irb(main):021:1> end => nil irb(main):022:0> h "jinson" Hello Jinson! => nil irb(main):023:0> h Hello World! => nil
  9. 9. Classes irb(main):024:0> class Greeter irb(main):025:1> def initialize(name = "World") irb(main):026:2> @name = name irb(main):027:2> end irb(main):028:1> def say_hi irb(main):029:2> puts "Hi #{@name}!" irb(main):030:2> end irb(main):031:1> def say_bye irb(main):032:2> puts "Bye #{@name}, come back soon." irb(main):033:2> end irb(main):034:1> end => nil
  10. 10. Create an Object irb(main):035:0> g = Greeter.new("Jins") => #<Greeter:0x16cac @name="JIns"> irb(main):036:0> g.say_hi Hi Jins! => nil irb(main):037:0> g.say_bye Bye Jins, come back soon. => nil
  11. 11. Cycling and Looping @names.each do |name| puts "Hello #{name}!" end #C Code for (i=0; i<number_of_elements; i++) { do_something_with(element[i]); }
  12. 12. Ruby & PHP - Similarities ● Ruby is dynamically typed, like in PHP, so you don’t need to worry about having to declare variables. ● There are classes, and you can control access to them like in PHP 5 (public, protected and private) ● Some variables start with $, like in PHP (but not all) ● There’s eval, too. ● You can use string interpolating. Instead of doing ”$foo is a $bar”, you can do ”#{foo} is a #{bar}”—like in PHP, this doesn’t apply for single-quoted strings. ● There’s heredocs ● Ruby has exceptions, like PHP 5 ● There’s a fairly large standard library ● Arrays and hashes work like expected, if you exchange array() for { and }: array('a' => 'b') becomes {'a' => 'b'}. ● true and false behave like in PHP, but null is called nil
  13. 13. Ruby & PHP - Differences ● There’s strong typing. You’ll need to call to_s, to_i etc. to convert between strings, integers and so on, instead of relying on the language to do it ● Strings, numbers, arrays, hashes, etc. are objects. Instead of calling abs(-1) it’s -1.abs ● Parentheses are optional in method calls, except to clarify which parameters go to which method calls ● Variables are references. ● There’s no abstract classes or interfaces ● Hashes and arrays are not interchangeable ● Only false and nil are false: 0, array() and "" are all true in conditionals. ● Almost everything is a method call, even raise (throw in PHP).
  14. 14. Questions...?

×