I am returning with another installment of the full stack web dev phase overview. These four weeks transitioned the content from front end to back end. The infrastructure chosen for this phase consisted of ruby, SQL (sqlite), Active Record, Rake (for db migrations) and Sinatra. Let's start with Ruby. My professional experience revolved around static languages of .Net and Java and it was cool. What can I say, I like being explicit in my creations. Ruby seems to be the opposite to this and I never considered its domain to be my cup of tea. Nevertheless there are a lot of positives as well. I love the emphasis on simplicity. I feel like there is not a single unnecessary character left in the language. Working with arrays is pure bliss thanks to a ton of short array methods that seem to be able to accomplish everything. Adding an element to an array is as simple as:
todays_menu << banana_basil_smoove
How cool is that!
Ok. Enough with personal memoires. Let's get technical.
Here is how to build string literals:
"#{$evil_monster} is trying to kidnap Princess Peach!"
Common debugger is pry
.
Install
gem install pry
Reference
require 'pry'
Use
binding.pry
Looping is short and simple
10.times do |i|
puts "Looping!"
puts "i is: #{i}"
end
Objects
Use @
symbol to make instance variable without declaring it anywhere.
class Dog
def name=(value)
@name = value
end
end
Attribute accessor macros
Use auto-implemented properties whenever possible for convenience and clarity
class Person
attr_writer :name
attr_reader :name
attr_accessor :age
end
Initializer methods or constructor
class Dog
attr_reader :breed
def initialize(breed)
@breed = breed
end
end
self
is equivalent to .Net's this
.
def get_adopted(owner_name)
self.owner = owner_name
end
self
used on method definition makes an equivalent of a static method and @@
makes a static variable in .Net
def self.count
@@album_count
end
To make a method private use private
private
def choose_liquor
@cocktail_ingredients << "whiskey"
end
Inheritance
class Car < Vehicle
end
To invoke code from inherited class use super
.
class User
def log_in
@logged_in = true
end
end
class Student < User
def log_in
super
@in_class = true
end
end
Top comments (0)