irb
ruby
command.rb
5.+(4) == 5 + 4
5.+(4) == 5 + 4
5.class
alot = 200 ** 200
puts alot
puts alot.class
counter += 1
counter = counter.next
5.to_s
5.to_f
.abs
.even?
5[0]
10.next
-1.abs()
puts 1.even?
puts 2.even?
print 5[0], 5[1], 5[2]
print 4[0], 4[1], 4[2]
10.50.ceil
10.50.round
10.50.to_i
10.50.to_r
10.50.to_i
10.5.to_r
10.49.round
10.49.ceil
#{varname}
!
at the end of the method namestr = "A string. A nother"
int = 10
str.upcase
str.capitalize
str.downcase!
puts str
str.length
str.ascii_only?
puts " soooo many spacess and such".squeeze
puts " soooo many spacess and such".squeeze(" s")
puts str.include?("ri")
puts str.include?("ro")
puts "Hello, you are #{str}"
puts "Hello, you are #{str.upcase}"
puts "Hello you are a float now: #{int.to_f}"
Array
constructorArray.new(capacitiy,initial)
var = [1, 2, 3, 4]
array[index,howmany]
array[start..end]
end
, could be -1
arr = [10,20,4,6,7,1,20,0,nil]
puts arr[0]
puts arr[-2]
puts arr[1,2]
puts arr[4,1]
puts arr[0..2]
puts arr[5..-1]
.push
/.pop
.compact
- removes nil
elements.uniq
.inspect
puts arr.compact
puts arr.uniq
puts arr.inspect
[1, 2, 4, 4] | [5, 6 ,7]
[1, 2, 4, 4] & [5 , 6, 7]
[1, 2, 4, 4] & [4, 10, 20]
[1 ,2 , 3 ,4 ,4 ] - [1]
Hash.new
, but is often initialized expclicitlyhash = {'key1' => 'val1' , 'key2' => 'val2', ....}
hash1 = {'google' => 'www.google.com', 'umbc' => 'umbc.edu',
'state of maryland' => 'md.gov'}
hash2 = Hash.new(0)
puts hash1
puts hash2
[]
) like arrays.keys
/ .values
.has_key?
.delete
.invert
puts hash1['google']
puts hash1['something']
puts hash2['not_here']
puts hash1.keys
puts hash2.has_key?("not_here")
hash2["not_here"] = 10
puts hash2.invert.inspect
hash3 = hash2.invert
puts hash3['missing']
then
is optional, but existselsif
case
when
if 10 > 11 then puts "HMMM" else puts "Seems good" end
a = 1
b = 2
if a > b
puts "#{a} is bigger than #{b}"
elsif a == b
puts "#{a} equals #{b}"
else
puts "#{a} is less than #{b}"
end
result = if a > b
"#{a} is bigger than #{b}"
elsif a == b
"#{a} equals #{b}"
else
"#{a} is less than #{b}"
end
puts "I have compared #{a} and #{b} and have determined that #{result}"
case "Hello"
when /^A/
puts "You start with an A"
when /^H/
puts "You start with an H"
else
puts "You start with something else"
end
number = 10
case
when number % 2 == 0
puts "#{number} is even"
when number % 2 == 1
puts "#{number} is odd"
end
for var in array
#Do something
end
arr = [1,2,4,"Thing",nil]
for el in arr
puts el
end
hash = {'today' => "Thursday", "tomorrow" => "Friday"}
for el in hash
puts el
end
hash = {'today' => "Thursday", "tomorrow" => "Friday"}
for k,v in hash
puts "#{k.capitalize} is #{v}"
end
do |vars| ... end
Intengers
.times
.upto
/.downto
Arrays and Hashes
.each
Strings
.each_line
.each_char
5.times {puts "Hi"}
5.upto(10) do |i|
puts i * i
end
5.downto(10) do |i|
puts i * i
end
hash = {'today' => "Thursday", "tomorrow" => "Friday"}
hash.each {|k,v| puts "#{k.capitalize} is #{v}"}
str_long = "This is a really long string\n I have put some new line characters\n to see what happens"
str_long.each_line do |l|
puts l.strip
end
str_long.each_char do |l|
puts l.strip
end
def
keyword$
yield
keyworddef square(i)
i * i
end
square(10)
def wrapper(i)
yield
end
wrapper(10) {puts "Hello"}
def wrapper_with_var
x = yield("Hello")
puts "Yield returned #{x}"
end
wrapper_with_var do |i|
puts "i was passed as #{i}"
i.downcase
end
system
will run the enclosed commands, returning true or falseIO.popen {block}
gets
data fromIO.popen("ls -lh *.html") do |stream|
while line = stream.gets do
parts = line.split(" ")
puts "#{parts[-1]} is #{parts[4]} big"
end
end
IO.popen("ls -lh *.html") do |stream|
stream.each do |line|
parts = line.split(" ")
puts "#{parts[-1]} is #{parts[4]} big"
end
end
class
keyword@
intialize
=
class TIME
def initialize(hour,min)
@hour = hour
@min = min
end
def hour
@hour
end
def hour=(nHour)
@hour = nHour
end
def to_s
"It's #{@hour}:#{@min}"
end
end
now = TIME.new(11,15)
puts now
+
, define +
in your classclass TIME
def min
@min
end
def +(anotherTime)
TIME.new(self.hour + anotherTime.hour,
self.min + anotherTime.min)
end
end
puts now + TIME.new(11,12)
class Array
def beMean
self[0] , self[-1] = self[-1], self[0]
end
end
arr = [1,43,9,68,19,6890,185,3]
arr.beMean
puts arr
gem
command line program is usually installed when installing Rubygem install packageName