Sunday, April 29, 2012

Sync RVM with TextMate

I've been trying to sync TextMate with RVM found this tutorial extremely helpful.

Basic Process:
  1. Print path to RVM-AUTO-RUBY ( the exec file that runs the default interpreter set by rvm )
    • Unix command: $ which rvm-auto-ruby
  2. Copy path to TextMate's preferences
    • Under TextMate > Preferences > Advanced > Shell Variables create a new shell variable 
    • Variable: TM_RUBY
    • Value: ( path returned by which rvm-auto-ruby cmd )
Debugging Tips:

Tuesday, April 24, 2012

1: Programming Review

Project description.
Concerns
  • line 21: I'm not sure if this is the best way to check the command line inputs and save them.
  • line 27: I feel like there should be a way to combine this with line 24? like or die in perl 
  • I can't believe the there aren't any Math.sum Math.average or Math.median functions but I do like the way you can easily insert methods into a core class ( line 1 - 11 )
  • line 31 - 36: I swear I've heard of an cleaner way to parse in a file, but my solution works 
  • line 40: I'm not really sure what ensure does
  • line 2: inject(:+) ??? what the crap? this is cool, but not intuitive at all. Someone please explain
  • I could make it a little more object oriented by making a print result method ... but meh
Let me know what you think. 

# Add sum, mean, & median to Array class
class Array; def sum; inject(:+) end end
class Array; def mean; sum.to_f / size end end
class Array
def median(arry)
sorted = arry.sort
medpt = arry.length / 2
medpt1 = ( ( arry.length - 1 ).to_f / 2 )
(sorted[medpt] + sorted[medpt1]).to_f / 2
end
end
def empty
puts "Sum: 0.0"
puts "Mean: 0.0"
puts "Median 0.0"
exit
end
begin
# Get file from cmd :: Default file is input1.txt
if ARGV[0] != nil; input = ARGV[0] else input = "input1.txt" end
#Read in a file
file = File.new(input, 'r')
# check file size
if File.size(input) <= 0; empty end
#Parse file
nums = Array.new
while (line = file.gets)
num = line.split(" ")
num.length.times do |n|
nums.push(Float(num[n]))
end
end
rescue Errno::ENOENT
puts "The file was not found: #{$!}"
exit
ensure
file.close if file
end
#Print out sum,mean & median
puts "Sum: #{sprintf("%.2f", nums.sum)}"
puts "Mean: #{sprintf("%.2f",nums.mean)}"
puts "Median: #{sprintf("%.2f",nums.median(nums) )}"
view raw Review.rb hosted with ❤ by GitHub

Blog init

Blog.what?
Here are some sample projects from my CIS 206 Discrete Mathematics class. The course is taught by Geoffrey Draper at BYU-Hawaii.  In our class, we are permitted to program in any language of our choice. I've decided to take advantage of that freedom to try and learn Ruby. I'm using Ruby 1.9.2

Blog.why? 
I've decided to blog my projects for the following reasons:

  • My programs will be helpful to others who learn Ruby
  • People's comments will help me learn Ruby faster
  • It's a personal programming journal
end