Ruby introduction
-
Upload
shrikanth-rajarajan -
Category
Technology
-
view
131 -
download
0
Transcript of Ruby introduction
MethodsNo return type needed
Paranthesis is optional
Ruby method returns the last line
Method is called
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