Skip to content

Instantly share code, notes, and snippets.

@LizardLeliel
Created November 7, 2014 17:42
Show Gist options
  • Save LizardLeliel/2282bc235286f2d1f09b to your computer and use it in GitHub Desktop.
Save LizardLeliel/2282bc235286f2d1f09b to your computer and use it in GitHub Desktop.
I plan to show this small code to a friend soon, to show him ruby
# ==== Basics =====
#This is how you comment in Ruby! Think # is like a //
puts "Hello, World!"
#Equivalent to System.out.println("Hello, World!");
print "Type something in: "
#Equivalent to System.out.print("Type something in: ")\
z = gets() # same as String z = kbd.nextLine(). z is automatically a string
print "You just typed: "
print z
# ==== If statements: ====
x = 5 # I just declared x, and set it to 5. x is automatically an int
if x < 2
puts "Less then two"
elsif x < 10
puts "Less then ten"
else
puts "higher then ten"
end
# Prints "Less then ten"
# ==== Variables can be reassigned any type =====
y = 4
puts y # Prints 4
y = "Hello World"
puts y # Prints "Hello World"
y = "Hey "*4
puts y # Prints "Hey Hey Hey Hey "
# You don't need to worry about variable type.
#Although, 3.1415/"Hey" will give you an error (the program will quit)
# ==== For loops ====
for i in (3..9) do
print (i.to_s + " ")
end
# Same as: for (int i=3;, i <= 9, ++i) { System.out.print(i); }
# Output: 3 4 5 6 7 8 9
# i.to_s turns i into a string
print "\n"
# ==== functions ====
def thisIsAFunction(y)
return y*4
end
# This is how you create a function in Ruby!
# You don't need to state their return type - no void, int, etc.
puts thisIsAFunction(5) # prints 20
puts thisIsAFunction("Hello ") # prints "Hello Hello Hello Hello"
gets() #Pause - this is the end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment