Ruby Notes

default

 
=begin
#
# Last resort way to view the source of a rails Methods: (ex: link_to)
git clone http://github.com/rails/rails.git
cd rails
grep -r 'def link_to'
#
=end


gem list
# List all gems installed if no name given

gem list r
# List all gems that start with r


# 3 ways to run system command
# ----------------------------

# 1) Using readlines
getDate = IO.popen("date")
readDate = getDate.readlines
print readDate

# 2) Using system()
system("date")
#system("tar xzf test.tgz")

# 3) Using backticks
result = `date`
print result



# Concatenate
# -----------

v = 'one' << 'two' << 'three'
print 'v = ' << v

w = 'one' + 'two' + 'three'
print 'w = ' +  w

x = "#{'one'}#{'two'}#{'three'}"
print "x = #{x}"

y = "one#{'two'}#{'three'}"
print "#{'y'} =  #{y}"

z = "onetwo#{'three'}"
print "z =  #{z}"


=begin
irb --simple-prompt
# Start irb with less cluttered prompt
=end

=begin
Weird looking multiline
comments in ruby
=end

# Single line comment

0.23 + 0.23
# 0 needs to be put in front to add .23 + .23

x="99".to_i
# Convert string to integer

print "pickle"
# Prints to the screen without a newline

puts "pickles"
# Prints to screen with a newline added; if not one already

p "please"
# Prints to screen as an inspect string

hungry = gets
# Use gets to grab input from keyboard and assign as variable


if hungry == "very"
puts "heres your pickle"
else
puts "not wasting any pickles on you!"
end
# Simple if statement; conditional blocks need to end with end

false;nil;true
# nil shows an absence of a result and will cause conditionals
# to fail.

# Types of variables: # local, instance, class, global
# Other identifiers are: constants, keywords, and method names

# Variables start with a-z or _ and consist of letters, underscores,
# and_or digits (convention is to use _ instead of camelCase

default