Programming And More
, //

Ruby entered my brain in 2004 when I bought a book about it. I was curious but obviously not curious enough to learn it so I basically don’t know a shit anymore and I’m sure I don’t even read the whole book (as always, hello scala!).

However, ruby is not so special if you already know another programming or scripting language and I started becoming interested again, so here we are:

assigning variables

my_super_cool_variable = "some string value"
puts my_super_cool_variable
print "6^2 = "+(6**2).to_s

Oh, see, converting between string and integer values is actually very easy. Just append .to_i or .to_s and you are done.

Arrays

cars = ["Audi","BMW","Mercedes","Lexus"]
p cars
puts cars[0]
puts cars
puts cars.length
puts cars.reverse
puts cars[0..cars.length]

for run in (0..cars.length)
puts cars[run]
end

Add elements to your array

cars = ["Audi","BMW","Mercedes","Lexus"]
p cars
cars << "Saab"
p cars



getting user input

car = gets
print car


reading a file

file_contents = File.read("/etc/resolv.conf")
print file_contents


writing to a file

print "enter your first name: "
file_handle = File.new("/tmp/foobar", "w")
first_name = gets
file_handle.puts first_name
file_handle.close

It asks for your first name and writes it to /tmp/foobar. “cat” the file after executing this script. Your name will be in it. Easy peasy!

simple functions / methods

def foobar
puts "foobar"
end

foobar # prints "foobar\n"
def greet(firstname, lastname)
puts "Welcome "+firstname+" "+lastname
end

greet("Andreas","Schipplock")
def greet(*family)
p family
end

greet("Oma","Opa","Tante","Cousine")



simple prototype based object orientation

car = Object.new

def car.drive
puts "driving along the road"
end

def car.speedup(speed)
puts "speeding up to "+speed.to_s+" km/h"
end

def car.mileage
return 120000
end

def car.started?
return false
end

def car.id
return self.object_id
end

def car.add_features(*features)
p features # p = print for arrays
end

def car.default_climate_value(temperature=20)
return temperature
end

car.drive
car.speedup(140)
puts car.mileage.to_s
puts car.started?.to_s
puts car.id.to_s
car.add_features("air cooling", "automatic transmission", "foobar")
puts car.default_climate_value.to_s
puts car.default_climate_value(25).to_s

if car.respond_to?("turn_turbo_on")
car.turn_turbo_on
else
puts "sorry, this car doesn't have a turbo"
end

print "what do you want the car to do now? -> "
method = gets.chomp

if car.respond_to?(method)
car.send(method)
else
puts "sorry, the car can't do that"
end

executing this script could look like this:

andreas_schipplock@andreas:~/projects/ruby$ ruby1.9.1 c2f.rb 
driving along the road
speeding up to 140 km/h
120000
false
74485320
["air cooling", "automatic transmission", "foobar"]
20
25
sorry, this car doesn't have a turbo
what do you want the car to do now? -> drive
driving along the road
andreas_schipplock@andreas:~/projects/ruby$



a simple class

class Car
def speedup
return true
end
end

audi = Car.new
if audi.speedup
puts "yo, dawg, I'm speeding up"
else
puts "eh, mother*, I'm an old lady"
end

a little more advanced example of a class

class Car
def initialize(build_year,mileage)
@build_year = build_year
@mileage = mileage
@ready = true
end

def speedup(speed)
@current_speed = speed
end

def current_speed
return @current_speed
end

def ready?
return @ready
end

def age
return (Time.new).year - @build_year
end

def mileage
return @mileage
end
end

audi = Car.new(1989,245000)

if audi.ready?
audi.speedup(140)
puts audi.current_speed
audi.speedup(220)
puts audi.current_speed
puts "age: "+audi.age.to_s
puts "mileage: "+audi.mileage.to_s
end

The variables with the @-sign are properties or instance variables that are available inside the class. The example demonstrates their state inside the class. The “initialize” method is the constructor and is called whenever you create a new instance “audi = Car.new”.

get and setter methods

class Car
def initialize(mileage)
@mileage = mileage
end

def mileage=(miles)
@mileage = miles
end

def mileage
@mileage
end
end

audi = Car.new(245000)
puts "old mileage: "+audi.mileage.to_s
audi.mileage = 246000
puts "new mileage: "+audi.mileage.to_s


You can shorten that:

class Car
attr_reader :mileage
attr_writer :mileage

def initialize(mileage)
@mileage = mileage
end
end

audi = Car.new(245000)
puts "old mileage: "+audi.mileage.to_s
audi.mileage = 246000
puts "new mileage: "+audi.mileage.to_s


And even shorter…

class Car
attr_accessor :mileage

def initialize(mileage)
@mileage = mileage
end
end

audi = Car.new(245000)
puts "old mileage: "+audi.mileage.to_s
audi.mileage = 246000
puts "new mileage: "+audi.mileage.to_s

“attr_reader :mileage” unfolds to:

def mileage
@mileage
end

“attr_writer :mileage” unfolds to

def mileage=(miles)
@mileage = miles
end

“attr_accessor :mileage” unfolds to

def mileage=(miles)
@mileage = miles
end

def mileage
@mileage
end



Inheritance

class Car
def type
@type = "rwd"
end
end

class Audi < Car
def type
@type = "awd"
end
end

my_car = Audi.new
puts my_car.type

Outputs “awd”.

Inheritance with constants

class Car
TYPES = ["AWD", "RWD", "FWD"]
def type
@type = self::TYPES[1]
end
end

class Audi < Car
def type
@type = Car::TYPES[0]
end
end

my_car = Audi.new
puts my_car.type
blog comments powered by Disqus
Latest Tweets