Tag Archives: Ruby

Computer Science Programming Basics In Ruby

Computer Science Programming Basics In Ruby
Originally submitted at O’Reilly

Product: Computer Science Programming Basics in Ruby by oreilly
Submitted at: O’Reilly

Great intro to basics!

by JimSchubert from Seattle WA on 6/7/2013

Pros: Accurate, Well-written, Helpful examples, Easy to understand, Concise
Best Uses: Student, Novice
Describe Yourself: Developer

I read this book as part of the O’Reilly blogger program. I have been a professional software engineer since 2008, and I thought that I wouldn’t gain much from this book from a technical perspective and just provide some creative feedback for future readers. I also regularly enjoy teaching others about technology and different aspects of development. I was very impressed with the authors’ presentation of material.

If you have a degree in computer science, this book is most likely not for you. The actual material discusses things like what an array is and how to use one. It also provides examples of how to use branching conditional structures, objects, files, etc; like the title says, these are the basics.

What impressed me the most was the use of diagrams throughout the book. As engineers we regularly try to cut corners by going light on documentation, which is a practice that plagues the field and turns our science into more of an art. What is even more difficult than writing documentation or learning materials for other engineers? Writing for non-engineers. This book does an excellent job of explaining concepts that most authors take for granted. For example, I think you would have difficulty finding an introductory level book that doesn’t explain arrays with a picture of adjacent boxes to represent indexes; as engineers we assume that new engineers may have issues with data structures, but we regularly take control flow for granted. I think we assume that control flow can be really explained by requesting it to directions, but this simplification may not work for everyone. This book presents control flow by providing an application example, then displaying the logic of the example in a flowchart. I love that. I think this aspect alone would easily reduce some of the intimidation a new student to the computer science field may have.

I would recommend this book to students or anyone considering a career move into software or web development. I would also personally be interested on reading more by this team of authors on advanced topics.

Flattr this!

Rails 3 + RSpec + Rake tasks are missing

Recently, I installed Rails 3 using rvm. I wanted to follow quickly through Rails 3 in Action so I could see the differences between Rails 2.3 that I’m familiar with and the new stuff in Rails 3.

I also wanted to follow along because the book uses a behavioral driven design technique. This is very different from how I usually code, so I was intrigued.

After installing everything properly, I had an issue with rake tasks for rspec being nonexistent. After a couple hours of tweaking and reinstalling things, I came across the answer:

gem update bundler

Yeah, that’s it. There is an issue with the version of bundler pulled out of ruby gems on a fresh install of rails 3.0.3.

Flattr this!

CopyCat Rails’ Time Extensions

I’ve become a fan of Ruby’s simple syntax. My favorite thing is the ActiveSupport’s Numeric Time Extensions. I’m starting a library of useful extensions, and I’m going to be adding a number of shortcuts to mock these extensions.

For those of you that don’t want to follow the link, check out the syntax:

  
  # equivalent to Time.now.advance(:months => 1)
  1.month.from_now

  # equivalent to Time.now.advance(:years => 2)
  2.years.from_now

  # equivalent to Time.now.advance(:months => 4, :years => 5)
  (4.months + 5.years).from_now

Here is what I’ve gotten so far (it only includes int for now):

public static class DateExtensionsThatMockRuby
    {
        /// <summary>
        /// Retrieve the number of ticks for x Days.
        /// <example>long ticks = 5.Days();</example>
        /// </summary>
        /// <param name="i"></param>
        /// <returns>ticks</returns>
        public static long Days(this int i)
        {
            return new TimeSpan(i, 0, 0, 0).Ticks;
        }

        /// <summary>
        /// Retrieve the number of ticks for x Hours.
        /// </summary>
        /// <example>long ticks = 15.Hours();</example>
        /// <param name="i"></param>
        /// <returns>ticks</returns>
        public static long Hours(this int i)
        {
            return new TimeSpan(0, i, 0, 0).Ticks;
        }

        /// <summary>
        /// Retrieve the number of ticks for x Minutes
        /// </summary>
        /// <example>long ticks = 97.Minutes();</example>
        /// <param name="i"></param>
        /// <returns>ticks</returns>
        public static long Minutes(this int i)
        {
            return new TimeSpan(0, i, 0).Ticks;
        }

        /// <summary>
        /// Retrieve the number of ticks for x Seconds
        /// </summary>
        /// <example>long ticks = 3000.Seconds();</example>
        /// <param name="i"></param>
        /// <returns>ticks</returns>
        public static long Seconds(this int i)
        {
            return new TimeSpan(0, 0, i).Ticks;
        }

        /// <summary>
        /// Retrieve the number of ticks for x Milliseconds
        /// </summary>
        /// <example>long ticks = 3000.Milliseconds();</example>
        /// <param name="i"></param>
        /// <returns>ticks</returns>
        public static long Milliseconds(this int i)
        {
            return new TimeSpan(0, 0, 0, 0, i).Ticks;
        }

        /// <summary>
        /// Retrieve a DateTime object from ticks
        /// <example>DateTime dt = 236423690923466.AsDateTime();</example>
        /// /// <example>DateTime dt = 10.Days().AsDateTime();</example>
        /// </summary>
        /// <param name="ticks"></param>
        /// <returns>DateTime</returns>
        public static DateTime AsDateTime(this long ticks)
        {
            return new DateTime(ticks);
        }

        /// <summary>
        /// Retrieve a DateTime object from ticks
        /// <example>DateTime dt = 236423690923466.AsDateTime(DateTimeKind.Utc);</example>
        /// /// <example>DateTime dt = 10.Days().AsDateTime(DateTimeKind.Utc);</example>
        /// </summary>
        /// <param name="ticks"></param>
        /// <returns>DateTime</returns>
        public static DateTime AsDateTime(this long ticks, DateTimeKind kind)
        {
            return new DateTime(ticks, kind);
        }
    }

Flattr this!

Example Ruby code

I decided to do some of those Facebook puzzles in Ruby. I won’t post everything I do, because I would consider that cheating (after all, you can win prizes!).

However, considering this is a very simple script, and I’m doing it more for learning Ruby than to enter into Facebook’s puzzles, I will post this one.

This is the first puzzle, called Hoppity Hop:

private
@file_exists = false
def get_integer(filename)
    if File::exists?(filename)
        file = File.open(filename)
        line = file.gets.strip!
        file.close
        @file_exists = true
        return line.to_i
    else
        @file_exists = false
        return 0 #zero files present
    end
end

def get_string(num)
    if(num%3 == 0 && num%5 == 0)
        print "Hop\n"
    elsif num%3 == 0
        print "Hoppity\n"
    elsif num%5 == 0
        print "Hophop\n"
    else
        return #nothing
    end
end

def hip_hop(arg)
    num = get_integer(arg)
    if(num > 0 && @file_exists)
        for i in 1..num.to_i
            get_string(i)
        end
    else
        puts "File not found"
    end
end

public

arg = ARGV[0].to_s # this is the name of the file
if(/\D/ =~ arg)
    puts "Please enter a positive integer as the filename."
else
    unless arg == nil
        hip_hop(arg)
    end
end

Flattr this!

Ruby on Rails, Aptana Studio

This weekend, I decided to give Ruby on Rails a go, considering I just finished school and I’ll have plenty of free time to explore.

I downloaded Aptana Studio and installed Ruby, Rails, PHP, and a few extra ruby gems for fun. Then, I went over to the Rails Guide to follow along and do some learnin’. At the end of the guide, I ran the completed blog application and received errors. A little googling helped me find the solution.
Apparently, Aptana doesn’t install the latest build of gems from its repositories, and the guide was written for the newest version.
As noodlygod writes on the forum linked above:

Hello, I just thought I’d post something as I had the same problem on Windows.

I tried running the command “gem install rails –source http://gem.rubyonrails.org” but I received the error: “actionpack requires rack (>= 0.9.0, runtime)”

So I ran “gem install rack” which installed something
And then “gem install rails –source http://gem.rubyonrails.org” and it installed 2.3.0 just fine.

Based on this post: http://railsforum.com/viewtopic.php?pid=89050
I also changed my rails version in environment.rb to “2.3.0” and renamed “application.rb” to “application_controller.rb” and everything is working. Thanks a bunch for the info!

Flattr this!