Sunday, January 6, 2013

return random word from array or list



def new_word #returns random word
    words = %w{red blue orange green black white purple grey}
    lenword = words.length
    word_index = rand(1..(lenword -1))
    puts words[word_index]
end

new_word

#=> *new word

Saturday, January 5, 2013

something.include?(obj)


words = %w{cat, dog, sheep, bird}
puts words.include?("dog")
#=> true

puts words.incldue?("elephant")
#=> false

puts "the quick brown fox jumps over the lazy dog".include?("fox")
#=> true

output: puts, print, p


puts

array = %w{quick brown fox}
array.collect {|x| puts x.capitalize}

# => 
# Quick
# Brown
# Fox

print

array = %w{quick brown fox}
array.collect {|x| print x.capitalize}

# => QuickBrownFox

p

array = %w{quick brown fox}
array.collect {|x| puts x.capitalize}

# =>
# "Quick"
# "Brown"
# "Fox"

Tuesday, January 1, 2013

load or require files into irb



First, determine ruby's loadpath on your computer by doing...
irb(main):001:0> $:
=> ["/usr/local/lib/site_ruby/1.9.1", "/usr/local/lib/site_ruby/1.9.1/x86_64-linux", 
"/usr/local/lib/site_ruby", "/usr/lib/ruby/vendor_ruby/1.9.1", 
"/usr/lib/ruby/vendor_ruby/1.9.1/x86_64-linux", 
"/usr/lib/ruby/vendor_ruby", "/usr/lib/ruby/1.9.1", 
"/usr/lib/ruby/1.9.1/x86_64-linux"]

If the file/s you "require" is not in ruby's loadpath, you need to specify them...
irb(main):004:0> Dir.glob('/home/j/aa/*.rb')
=> ["/home/j/aa/mmbiz.rb", "/home/j/aa/a.rb"]
irb(main):005:0> require "/home/j/aa/mmbiz.rb"
This is a test
=> true