Ruby introduction

30
Ruby Introduction

Transcript of Ruby introduction

Ruby Introduction

print “Hello World”

Hello World

Most things in Ruby isObject

OObject of class String

OObject of class Fixnum

Conditionals

or elsif admin == false

paranthesis is optional

MethodsNo return type needed

Paranthesis is optional

Ruby method returns the last line

Method is called

Loops

Arrays

Inserting into an Array

Deleting from an Array

Looping through an Array

Hash

KEY Value

Based on key, you can get the value

Changing the value

Adding a new Key value pair

Symbols

How it differs from a String

Saves memory and remains in memory always

Symbols can be used as keys in Hash

Class Constructor

Method

Inheritance

➢ Global - $➢ Instance - @➢ Class - @@➢ Local - [a-z] or _➢ Constants - CAPS

Variables

Global Variables$myvar = “Its is a Place”

class Chennaidef what_is_that

puts $myvarend

endclass NewYork

def what_is_thatputs $myvar

endend

Output: chennai = Chennai.new newyork = NewYork.new

chennai.what_is_that newyork.what_is_that

--------------------------------------Its is a PlaceIts is a Place

Instance Variablesclass Place def initialize(place)

@current_place = place end def show_me

puts @current_place endend

Output: chennai =Place.new(“Chennai”) newyork = Place.new(“New York”)

chennai.show_me newyork.show_me

--------------------------------------ChennaiNew York

Class Variables@@hotels=0

class Place def more_hotels @@hotels +=1 endend

Output: chennai =Place.new newyork = Place.new

chennai.more_hotelsnewyork.more_hotels

--------------------------------------12

Local VariablesOutput: chennai =Place.new(“Chennai”) newyork = Place.new(“New York”)

chennai.show_me newyork.show_me

--------------------------------------ChennaiNew York

Local Variable

class Place def initialize(place)

@current_place = place end def show_me

puts @current_place endend

Constantsclass Place

MYVAR = 100 def show_me

puts “Value of constant is #{MYVAR}” endend

Output: chennai =Place.new(“Chennai”)

chennai.show_me

--------------------------------------Value of constant is 100

a version manager (RVM or rbenv or other)

Install ruby using

Thank You!