In 1984 the personal computer industry was forever changed by the first Mac. More expensive and less familiar than the DOS-based computers that were gaining popularity, the Mac was a first: It shipped with a point-and-click mouse standard and its core operating system – the thing we used to tell the computer what to do – was a flat desk-like surface. Once we got used to the idea, we could move the pointer around with the mouse, move "icons" that represent ideas on our virtual desk from one place to another.

There are three ways to invoke a method. Most of the time you'll probably only need #1, but #2 and #3 are used when you are doing something called metaprogramming – calling methods based on dynamic information.

1. object = Object.new
puts x.some_method
#=> 282660

2. puts x.send(:some_method)
#=> 282660

3. puts x.method(:some_method).call
#=> 282660

Reads a file line by line into an array my_stuff

my_stuff = []
file = File.new("config/random_categories.txt", "r")
while (line = file.gets)
my_stuff << line.chop!
end
file.close
my_stuff

Pass file to block

File.open("my_file.rb", "r") do |infile|
while (line = infile.gets)
puts "#{counter}: #{line}"
counter = counter + 1
end
end

Read File with Exception Handling

counter = 1
begin
file = File.new("readfile.rb", "r")
while (line = file.gets)
puts "#{counter}: #{line}"
counter = counter + 1
end
file.close
rescue => err
puts "Exception: #{err}"
err
end
© 2012 Tech Notes Suffusion theme by Sayontan Sinha