My Secret Life as a Spaghetti Coder
home | about | contact | privacy statement
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!


Comments
Leave a comment

There are no comments for this entry yet.

Leave a comment

Leave this field empty
Your Name
Email (not displayed, more info?)
Website

Comment:

Subcribe to this comment thread
Remember my details
Google
Web CodeOdor.com

Me
Picture of me

Topics
.NET (19)
AI/Machine Learning (14)
Answers To 100 Interview Questions (10)
Bioinformatics (2)
Business (1)
C and C++ (6)
cfrails (22)
ColdFusion (78)
Customer Relations (15)
Databases (3)
DRY (18)
DSLs (11)
Future Tech (5)
Games (5)
Groovy/Grails (8)
Hardware (1)
IDEs (9)
Java (38)
JavaScript (4)
Linux (2)
Lisp (1)
Mac OS (4)
Management (15)
MediaServerX (1)
Miscellany (76)
OOAD (37)
Productivity (11)
Programming (168)
Programming Quotables (9)
Rails (31)
Ruby (67)
Save Your Job (58)
scriptaGulous (4)
Software Development Process (23)
TDD (41)
TDDing xorblog (6)
Tools (5)
Web Development (8)
Windows (1)
With (1)
YAGNI (10)

Resources
Agile Manifesto & Principles
Principles Of OOD
ColdFusion
CFUnit
Ruby
Ruby on Rails
JUnit



RSS 2.0: Full Post | Short Blurb
Subscribe by email:

Delivered by FeedBurner