Verse Of The Day

Thursday, May 24, 2007

Ruby Syntax Sampler Script

Alright, I got a few different Ruby On Rails applications off the ground with the help of some tutorials, examples, and just plain screwing around. But I came to the scary conclusion that these apps were cool, but I still don't know much about Ruby the language. Rails makes it almost too easy! I only injected a few lines of code here and there based on the samples and tutorials to do things like define the foreign keys to get a selection list instead of text entry box for referenced tables.

So here's my lame attempt at playing with "Ruby proper" -- the language, devoid of frameworks. This is my trashy syntax_sampler.rb script that demonstrates some of the basics of Ruby.

require 'net/http'

#define a class
class SyntaxSampler
# define a constant
Dude = "Constants are capitalized. They cannot be declared in methods"
# the constructor method in Ruby is always "initialize"
def initialize( name )
@name = name # instance var starts with at sign

def print_name
puts @name

# big ugly method
def dostuff
print "x = "
x = 5
puts x
print "x + x = "
puts x + x
print "x * x = "
puts x * x
print "x**x = "
puts x**x
print "x/2 is "
puts x/2
print "but x/2.0 is "
puts x/2.0
names = ['robb', 'paula', 'raina', 'taryn'] #assign array to 'names'
print "crappers reversed is "
puts "crappers".reverse
print "\nPrint the Array: #{names}" # embed values right in print instead of 2 lines
puts "\nReverse the Array: #{names.reverse}"
puts "for methods with no args, parenthesis optional..."
puts names.reverse()
print "\nCapitalize Each Array Item: "
names.each {|name| print "#{name.capitalize} " }
print "\nUpper case Each Array Item: "
names.each {|name| print "\n\t - #{name.upcase} " }
puts "\n\n'5' as an integer = #{"5".to_i}"
puts "\n19.0 as a string = #{19.0.to_s}"
puts "\n'a19' as an integer = #{"a19".to_i}"
puts "\n'19a3' as an integer = #{"19a3".to_i}"
puts Dude
# define a global var
$wicked = "Global vars start with a dollar sign"
puts $wicked

def anothermethod
puts $wicked # make sure that var is really global scope

def mirror( something )
puts "#{something} #{something.reverse}"

def mirrordefval( something = "dude")
puts "#{something} #{something.reverse}"

class DiffClass
def initialize

def rundiff
puts $wicked # really is global, even across classes
10.times {print " x "} # everything is an object
# use the above "required" module to do something...
Net::HTTP.start( '', 80 ) do |http|
print( http.get( '/en/LICENSE.txt' ).body )

# shortcut for string arrays, no more pesky commas and quotes
food = %w{elk deer caribou squirrel pig chicken cow}
puts food

# basic if/else control structure
count = 5
if count > 10
puts "Try again"
elsif count == 5
puts "You lose"
puts "Enter a number"

# a while loop
xx = 0
while xx < 10
print "#{xx}..."
xx += 1
print "done"

# classes and methods are defined, now run them....
ss ="Robb")
ss.mirror("hello, I'm passing params baby") # with parens
ss.mirrordefval "yo yo yo" # no parens
ss.mirrordefval # no params sent in, use default value
puts $wicked # still don't believe me? it IS GLOBAL DAMMIT!
dc =
#now a little introspection for your sorry ass
puts "\n\nMethods of SyntaxSampler class...."
SyntaxSampler.instance_methods.each {|mm| print "#{mm} "}
puts "\n\nNow eliminate inherited methods...."
# this looks like a simpler way to print out a list, eh?
puts SyntaxSampler.instance_methods(false).join(" ")
exit # not really needed here

1 comment:

All Blog Spots said...

great blog, keep the good work going :)