Bin-Blog logoBin-Blog

Learn about the latest in Web Development – as soon as I do.

Hello Script for Ruby

By • May 1st, 2008 • Category: Ruby, Scripts, Tutorials

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

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!"
	print "Anything wrong with your time machine? You have not gone anywhere, kiddo."
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
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
hello_string = hello("Binny")
print "Function call returned '" + hello_string + "'\n\n"

# One for the OOP fanboys - Class, members, object and stuff.
class Movie
	@name = ''
	@rating = 0
	def initialize(name)
		@name = name
	def rateMovie()
		@rating = (@name.length % 10) + 1 #IMDBs rating algorithm. True story!
	def printMovieDetails()
		print "Movie : " + @name + "\n"
		print "Rating : " + '*' * @rating + "(" + @rating.to_s + ")\n\n"
# Create the object
ncfom ="New Country for Old Men") #It's a sequel!

# File IO
# File reading, easy method...
file_in ='Hello.rb', 'r')
contents =
print "Hello has " + contents.length.to_s + " chars\n"

# Writing to a file
file_out ='/tmp/hello.txt', 'w')
file_out.print "Hello World from Ruby."
# 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
doXTimes(5) {|count| 
	print count.to_s + "th Time\n"

One Response »

  1. Caleb says:

    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.


Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>