Learning Ruby

So i recently decided to take a close look into Ruby, because i was reading through Richardson Dackam’s Blog post “How I TRAINed to learn Rails” and i really liked the way he approached learning to programm.

I’ll follow his lead to learn Ruby and then Rails. I start by reading “Learn Ruby the Hard way”. From what I’ve heared its a more practical way with little tasks throughout the book that can be quite a challenge for total beginners. As i took the “Odin Project’s” path once i came across the book called “Learning Programming” by Chris Pine, so i allready have a decent grasp on Ruby allready.

 

Writing a ruby program

  1. mkdir ruby
  2. cd ruby
  3. subl
  4. make new file and save as ‘ex1.rb’
  5. writing simple code like: puts hello world
  6. run with: ruby ex1.rb

 

yay, that’s easy 🙂

 

Using math operators

puts “I will now count my chickens:”

puts “Hens #{25 + 30 / 6}” #string with embedded ruby computation using #{}
puts “Roosters #{100 – 25 * 3 % 4}” #math

puts “Now i will count the eggs:”

puts 3 + 2 + 1 – 5 + 4 % 2 – 1 / 4 + 6

puts “Is it true that 3+2<5-7?” #a string, not boolean

puts 3+2<5-7 #boolean aka true or false

puts “What is 3 + 2? #{3+2}”
puts “What is 5 – 7? #{5-7}”

puts “Oh, that’s why it’s false.”

puts “How about some more.”

puts “Is it greater? #{5 > -2}”
puts “Is it greater or equal? #{5 >= -2}”
puts “Is it less or equal? #{5 <= -2}”

 

 

Using variables

 

cars = 100       #assigning variables to numbers
space_in_a_car = 4.0
drivers = 30
passengers = 90
cars_not_driven = cars – drivers      #substracting two variables with each other
cars_driven = drivers      #setting a new variable with allready used number
carpool_capacity = cars_driven * space_in_a_car
average_passengers_per_car = passengers / cars_driven

puts “There are #{cars} cars available”     #using variable to express its value = 30 in a string
puts “There are only #{drivers} drivers available”
puts “There will be #{cars_not_driven} empty cars today”
puts “We can transport #{carpool_capacity} people_today”
puts “We have #{passengers} to carpool today”
puts “We need to put about #{average_passengers_per_car} in each car.”

 

More variables

 

name = ‘Mark’
age = 26     #not a lie
cm = 2.54
kg = 0.453592
height = 74 * cm    #inches to centimeter
weight = 180 * kg    # lbs to kg
eyes = ‘Blue’
teeth = ‘white’
hair = ‘Brown’
puts “Let’s talk about #{name}”
puts “He’s #{weight} kilogramm heavy.”
puts “He’s #{height} centimeters tall, thats #{height / 100.0} meters”
puts “Actually that’s not too big”
puts “He’s got #{eyes} eyes and #{hair} hair”   #variables as words
puts “His teeth are usually #{teeth} depending on the coffee”

# this line is a trick, try to get it exactly right

puts “If i add #{age}, #{height}, and #{weight} I get #{age + height + weight}.”

 

And more…

 

types_of_people = 10
x = “There are #{types_of_people} types of people”
binary = “binary”
do_not = “don’t”
y = “Those who know #{binary} and those wo #{do_not}”    #strings inside a string

puts x
puts y

puts “I said: #{x}”    #another string in a string
puts “I also said: ‘#{y}’.”

hilarious = false
joke_evaluation = “Isn’t that joke so funny?! #{hilarious}”

puts joke_evaluation

w = “This is the left side of …”
e = “a string with a right side.”

puts w + e

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s