Hello Script is a file that contains the most commonly used elements of a programming language so that it can be used as a cheatsheet when working with that language. This Hello Script for Ruby is the sixth post in this series.
I have some experience with Ruby. I will not call myself an expert – but I am comfortable with Ruby. I like ruby. I even have written a few applications in it.
Hello Code
#!/usr/bin/ruby
print "Hello World\n"
name = "Binny"
year = 2008
print "Hello, " + name + " - welcome to " + year.to_s + "\n"
if (year > 2008) then
print "Welcome to the future - yes, we have flying cars!"
elsif (year < 2008) then
print "The past - please don't change anything. Don't step on any butterflies. And for the sake of all that's good and holy, stay away from your parents!"
else
print "Anything wrong with your time machine? You have not gone anywhere, kiddo."
end
print "\n\n"
# For loop like structure
0.upto(3) { |i|
print i.to_s + ") Hi there!\n"
}
print "\n"
# Numerical array
rules = ['Do no harm', 'Obey', 'Continue Living']
i = 0
while i<rules.length do
print "Rule " + (i+1).to_s + ": " + rules[i] + "\n"
i = i+1
end
print "\n"
# Associated arrays
associated = {
'hello' => 'world',
'foo' => 'bar',
'lorem' => 'ipsum'
}
associated.each { |key,value|
print key + " : " + value + "\n"
}
print "\n"
# Using Join and Split
csv_values = "hello,world,how,are,you\n".split(",")
print csv_values.join(":")
# Function, argument, return, call
def hello(name)
return "Hello " + name
end
hello_string = hello("Binny")
print "Function call returned '" + hello_string + "'\n\n"
# One for the OOP fanboys - Class, members, object and stuff.
class Movie
public
@name = ''
@rating = 0
def initialize(name)
@name = name
self.rateMovie()
end
def rateMovie()
@rating = (@name.length % 10) + 1 #IMDBs rating algorithm. True story!
end
def printMovieDetails()
print "Movie : " + @name + "\n"
print "Rating : " + '*' * @rating + "(" + @rating.to_s + ")\n\n"
end
end
# Create the object
ncfom = Movie.new("New Country for Old Men") #It's a sequel!
ncfom.printMovieDetails()
# File IO
# File reading, easy method...
file_in = File.new('Hello.rb', 'r')
contents = file_in.read
file_in.close
print "Hello has " + contents.length.to_s + " chars\n"
# Writing to a file
file_out = File.new('/tmp/hello.txt', 'w')
file_out.print "Hello World from Ruby."
file_out.close
# Command Executing
print `ls` #Execute the command 'ls' and print its output
print "\n"
# Regular Expressions
string = "Hello World"
print "Yup - its evil\n" if(/^Hell/.match(string))
print string.gsub(/l([^l])/, '\1') #Remove an 'l' from both words. Should print 'Helo Word' - The second arg must be in single quotes
print "\n"
# Some special/only-in-ruby stuff...
# Using a library
require "fileutils"
#Using yield/code blocks...
def doXTimes(i)
0.upto(i) {|count|
yield count+1
}
end
doXTimes(5) {|count|
print count.to_s + "th Time\n"
}
I found your script very helpful, but i just noticed something in the OOP section. Is it necessary to declare the instance variables in Ruby? Why not just initialize rating along with name. I’m new to Ruby so i hope you can clarify this point.
Thanks