In case you haven't bought the
2nd Edition Pickaxe or found the
1st Edition of Programming Ruby: The Pragmatic Programmer's Guide, I'll be covering some of the basics of Ruby here. I've got both, but I haven't looked at the first edition in a couple of months, and haven't yet found the time to open the second edition and give it the attention it deserves.
But, I have been in class, so I thought I might go over some of the simple things in Ruby, with no real method to my madness. I'll just
be going through different things as I remember them. Enjoy the lack of structure. You could actually copy and paste that into an IDE or .rb file and run it as-is if you wanted.
# the hash/pound/number symbol gives us a comment
# set a variable
val = 10;
#semicolons are optional per line. if you want more than one statement
#per line, you'll have to use it
something = 1; something_else = 2
puts something, something_else
puts '--------------------------'
# while loop putting 9 down to 0
while val > 0
#'puts' puts a string to the console
puts val = val-1
end
puts '--------------------------'
# or you can do
val = 10
puts val = val -1 while val > 0
puts '--------------------------'
val = 10
puts val = val -1 until val < 1
puts '--------------------------'
for i in 1..10 # .. shows us the range object
puts i
end
puts '--------------------------'
9.upto(10) { |v| puts v }
puts '--------------------------'
9.downto(1) { |v| puts v }
puts '--------------------------'
condition = true
if condition
puts "yes"
puts "yoho"
else
puts "no"
end
puts '--------------------------'
puts "yes" if condition
puts "no" unless condition
puts '--------------------------'
class Car #define a class called Car. Must begin with an uppercase letter
attr_reader :miles, :fuel_level #like getMiles but auto-done for you
attr_writer :fuel_level #like setFuelLevel, but can use on LHS of equation
# you could also use:
# def fuel_level=(lvl)
# @fuel_level = lvl
# end
def initialize (year)# is called when an object is created
@year = year # @varname lets us know that it is a member variable
@miles = 0
@fuel_level = 100
end
def drive #define a method called drive
puts "we are driving the car for one mile"
@miles += 1
@fuel_level -= 1
end
end
c1=Car.new(2006)
c1.drive
puts c1.fuel_level
# attr_writer example
puts c1.fuel_level=29
# the last statement is the one returned from a method
puts c1.drive
#attr_reader example
puts c1.miles.to_s + " miles driven" #to_s = toString
# one of my only beefs so far is that you cant simply do
# puts 1 + "sam"
puts '--------------------------'
# inheritance is done via the less than symbol <
class SoupedUpCar < Car
def drive # will drive 3 times as fast with only 2 times the gas consumption
@miles+=2
@fuel_level -= 1
super
end
end
c2 = SoupedUpCar.new(2007)
puts c2.inspect #gives us info on the object (puts is there for output only)
puts c2.class #tells us the class of the object
#add a method to SoupedUpCar
class SoupedUpCar
def burnout
puts "smell the rubber?"
end
end
#notice c2 has not been reinstantiated or anything
c2.burnout
Thats all for now. I'm starting to think I should have organized the code a little better. Maybe next time. I certainly need a colorizer for it.
Hey! Why don't you make your life easier and subscribe to the full post
or short blurb RSS feed? I'm so confident you'll love my smelly pasta plate
wisdom that I'm offering a no-strings-attached, lifetime money back guarantee!
Leave a comment
There are no comments for this entry yet.
Leave a comment