Sitemap
Zainal Mustofa

Zainal Mustofa

Full Stack Ruby On Rails Developer

Ruby basic

2 min readDec 11, 2014

--

Local Variable: name fish_and_chips x_axis thx1138 _x _26
Instance Variable: @name @point_1 @X @_ @plan9
Class Variable: @@total @@symtab @@N @@x_pos @@SINGLE
Global Variable: $debug $CUSTOMER $_ $plan9 $Global
Class Name: String ActiveRecord MyClass
Constant Name:FEET_PER_MILE DEBUG

Array and Hash

Array and Hash are order with indexed collections, accessing using key in array with integer for key, and hash provide more flexibility for key.

create array

a = [1,2,3,4] =># [1,2,3,4]

b = [‘a’,’b’,’c’]=>#[‘a’,’b’,’c’]

ary = Array.new #=> []
Array.new(3) #=> [nil, nil, nil]
Array.new(3, true) #=> [true, true, true]

Sometimes creating arrays of words can be a pain, what with all the quotes and commas. we can use %w for shortcut in ruby, that:

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”

Hash A Hash is a dictionary-like collection of unique keys and their values. Also called associative arrays, they are similar to Arrays, but where an Array uses integers as its index, a Hash allows you to use any object type.

make a hash:

inst_section = {
‘cello’ => ‘string’,
‘clarinet’ => ‘woodwind’,
‘drum’ => ‘percussion’,
‘oboe’ => ‘woodwind’,
‘trumpet’ => ‘brass’,
‘violin’ => ‘string’
}

inst_section = {
:cello => ‘string’,
:clarinet => ‘woodwind’,
:drum => ‘percussion’,
:oboe => ‘woodwind’,
:trumpet => ‘brass’,
:violin => ‘string’
}

inst_section = {
cello: ‘string’,
clarinet: ‘woodwind’,
drum: ‘percussion’,
oboe: ‘woodwind’,
trumpet: ‘brass’,
violin: ‘string’
}

Class, Objects, and Variables

Classes in Object Oriented basic structure in which there is a method in the class attributes and methods of the base class objects

inherited (subclass)

Get Zainal Mustofa’s stories in your inbox

Join Medium for free to get updates from this writer.

Callback invoked whenever a subclass of the current class is created.

class Foo
def self.inherited(subclass)
puts "New subclass: #{subclass}"
end
end

class Bar < Foo
end

class Baz < Bar
end

result:

New subclass: Bar
New subclass: Baz

--

--

Zainal Mustofa
Zainal Mustofa

Written by Zainal Mustofa

Full Stack Ruby On Rails Developer