Everyday Rails
-
Upload
netguru -
Category
Technology
-
view
378 -
download
1
Transcript of Everyday Rails
Blaze HadzikRuby on Rails [email protected]
:014 > 1.class => Fixnum :015 > (2.2).class => Float :016 > [].class => Array :017 > "Politechnika Slaska".class => String :018 > nil.class => NilClass :019 > “abc” + “d” => “abcd”
variables
type local instance class global constant
example name @name @@name $name NAME
you don’t have to specify variable type
Variables
a = 12a.class # => Integer
a = “polsl”a.class # => String
a = [‘a’, ‘b’, ‘c’]a.class # => Array
Arrays and Hashes
a = [ 'ant', 'bee', 'cat', 'dog', 'elk' ] a[0] # => "ant"a[3] # => "dog"# this is the same:
a = %w{ ant bee cat dog elk } a[0] # => "ant"a[3] # => "dog"
Arrays and Hashes
my_hash = {building: ‘school’,fruit: ‘orange’
}
puts my_hash[:building] # => ‘school’
Symbols
LOW_PRIORITY = 0HIGH_PRIORITY = 1
priority = HIGH_PRIORITY
vs
priority = :high
Symbols are simply constants that you don’t have to predeclare and that are guaranteed to be unique.
Classes, objects
class Schooldef initialize(name)
@name = nameend
end
school = School.new(‘polsl’)p school # =>#<School:0x007fa301836160 @name="polsl">
Class Attributesclass School
def initialize(name)@name = name
end
def name=(name)@name = name
end
def name@name
endend