If you are reading other books on Ruby, Rails, etc., . . .

Posted by john in Ruby on Rails, Ruby, Announcements No Comments »

If you’re reading other books, articles, blogs, on Ruby, Rails, etc., by all means post a review or comment in our books section. If you find a resource that is truly outstanding, I’ll get it and add my own review to our annotated list of resources.

Also, I notice that I hadn’t actually published that “John’s To-dos” page. It’s there now.

Your one-liners?

Posted by john in Ruby No Comments »

Now that we are past the one-liner era in our progress along the Ruby Way, I’d like to invite you to propose some one liners of your own.

Just to kick it off:

Given a dictionary d with bad words:

d = [ ‘Yankees’, ‘Rockies’, ‘Indians’ ]

write a one-liner that replaces any usage of such bad words in a sentence with the first letter of the bad word followed by stars, one star for each of the remaining letters.

Input:

“As much as I can’t stand the Yankees and relish beating them, I’m glad we faced the Indians.”

Output:

“As much as I can’t stand the Y****** and relish beating them, I’m glad we faced the I******.”

P.S. I haven’t implemented this one. Seems like a one-liner . . .

Perlisms in Ruby

Posted by john in Ruby No Comments »

Here’s a link for some Perlisms in Ruby. There are of course many more that those the author lists here:

http://blog.nicksieger.com/articles/2007/10/06/obscure-and-ugly-perlisms-in-ruby

The whole discussion is very interesting. I think the author is being awfully harsh on the defined? method, because it is easy to imagine scenarios where you’re having a little trouble determining if Ruby is understanding something as a method or constant.

Ruby Debugger

Posted by amy in Ruby on Rails, Ruby No Comments »

Someone in my section asked about debuggers for ruby code, and I said something unhelpful like “What’s wrong with puts?!” but promised to get back to them with something more useful. So if you want to, go read about Ruby-Debug here.

Today is Blog Action Day

Posted by john in Ruby No Comments »

As you may know, today is Blog Action Day, and one is supposed to write a post about the environment.

So I’m going to do my part.

If you have an environment variable in your operating system, such asĀ  “Path” you can get access to it in Ruby with ENV[’varname’]. So, in this case, you’d ask for ENV[’Path’]. (On Linux and Mac OS and even some Windows machines, it would be ENV[’PATH’].)

It doesn’t seem like a very worthy topic for Blog Action Day, but there it is. Enjoy!

Musing about Mixins

Posted by amy in Ruby No Comments »

Here’s a blog post that does a nice explanation of the difference between ruby module mixins and Java interfaces:

Interfaces in Java and .NET are exactly like legal contracts (where the compiler is the all-powerful judge). If you implement an interface yet don’t meet the terms of the contract, the judge refuses to allow you to proceed.

Mixins in Ruby are more like promises than contracts. When you agree to pick up a friend at the airport, you don’t sign a contract, you promise you’ll be there. The punishment is purely social: if you break a promise, your friends won’t do stuff for you. Here’s an example.

The blog entry then gives an example using Comparable, so if you’re having trouble grokking mixins, go read the rest.

Instances of the class Class

Posted by john in Ruby No Comments »

Awhile ago, a student wrote me with a question about an instance variable used right after the class declaration, i.e., inside of a method. Here’s how I summed up this puzzler in code to the group:

class Example  # The following line is the issue. It looks like a instance variable has been set to
  # a value, but it isn't visible later on.
  @var1 = [ 1, 2 ]
  p "Just inside 'class Example' but outside method, @var1 = #{@var1.inspect}"
#  p "Just inside 'class Example' but outside method, self = #{self}"

def initialize
    p "Inside ' Example.initialize', @var1 = #{@var1.inspect}"
#    p "Inside 'Example.initialize', self = #{self}"
@var2 = [ 3, 4 ]
end

def dump
    p "Inside ' Example.dump', @var1 = #{@var1.inspect}"
    p "Inside 'Example.dump', @var2 = #{@var2.inspect}"
#    p "Inside 'Example.dump', self = #{self}"
  end

end

Example.new.dump

class Example
  p "Re-opened class Example, outside method, @var1 = #{@var1.inspect}"
#  p "Re-opened class Example, outside method, self = #{self}"
end

If you remove the comments for the statements showing the value of self in various places, you will find that the value of self just inside of the declaration of the class is the name of the class, i.e., Example. Therefore, it would seem, @var1 is an instance of Example. What does this mean?

Ruby is more fully object-oriented that most other languages: Its model is much closer to, say, SmallTalk than it is to Java.

In Ruby, when we declare a class, we are creating a constant (note that capital letter for “Example”) that is a reference to a single object that is an instance of the class… Class. And how can we begrudge this instance its own instance variables? The syntax is a bit confusing, because we are used to variables prefixed by @ to be instance variables of instances of that very class we’re declaring (i.e., instances of Example). But those instance variables are referenced inside methods.

Hal Fulton says: “Variables starting with a single @, defined inside a class, are generally instance variables. However, if they are defined outside any method, they are really class instance variables.” He goes on to say: “Class instance variables cannot be referenced from within instance methods and, in general, are not very useful” (The Ruby Way, pp. 56-57).

Now, let us return to something we talked about briefly in lecture. Remember “extend”? That was weird, wasn’t it? We were able to bring in stuff from a module to a specific instance. Well where the heck does that stuff live? Well, in the module. But it suggests that maybe we could add methods to a specific object, not just to a class. Ruby facilitates this.

Again, Fulton (pp. 61-62):

A singleton class in Ruby is the classlike entity where methods are stored that are per-object rather than per-class. It is arguably not a “true class” because it cannot be instantiated. The following is an example of opening up the singleton class for a string object [listen to what Fulton is saying: Opening up a specific object, not the class] and adding a method:

str = "hello"
class << str      # Alternatively:
  def hyphenated  # def str.hyphenated
    self.split('').join('-')
  end
end

p str.hyphenated

…Because the method hyphenate exists in no other object or class, it is a singleton method on that object…

But remember that in Ruby, a class is itself an object. Thus we can add a method to the singleton class of a class, and that method will be unique to that object, which happens to be a class. Here is an example:

class MyClass
  class << self   # Alternatively: def self.hello
    def hello     # or: def MyClass.hello
      puts "Hello from #{self}!"
    end
  end

end
MyClass.hello

But you will notice that this is simply what we call a class method in Ruby. In other words, a class method is a singleton method on a class. We could also say it’s a singleton method on an object that happens to be a class.

Whew. Let us return now to Fulton’s claim that class instance variables are not very useful. I think they are sometimes. Suppose you wanted to track object creation. You might write something like this:

class Example1
  @@number_of_instances = 0
  def initialize
    @@number_of_instances += 1
    print "There have been #{@@number_of_instances} instances of Example1 created.n"
  end
  def num_instances
    @@number_of_instances
  end
end

e1a = Example1.new
e1b = Example1.new

Clear enough. But is it right that the instance can see the number of instances of the class? Shouldn’t we be able to keep that number private to the class? Yes we can. We can keep the number of instances in a class instance variable:

class Example2

  @number_of_instances = 0

  class << self
    def bump
      @number_of_instances += 1
      print "There have been #{@number_of_instances} instances of Example2 created.n"
    end
  end

  def initialize
    Example2.bump
  end

end

e2a = Example2.new
e2b = Example2.new

Notice here I do have to call the class method bump to get my counter incremented. But at least now the total number of instances is private to the class instance. Is there a way to get rid of the need to call Example2.bump? Yes there is, but you would have to override Class.new. I will leave that as an exercise for the reader. :-)

Ruby-ize this

Posted by amy in Ruby 3 Comments »

If you’re interested in making your code more ruby-esque, ruby fleebie has an ongoing feature called Ruby-ize this.

My Ruby and Rails RSS feeds

Posted by john in Ruby on Rails, Ruby No Comments »

So my last posting on feeds was everything but my Ruby/Rails RSS feeds and engineering feeds.

Here are my biggies.

General engineering / software / project management

  • Rands in Repose (Already mentioned)
  • Joel on Software - (Also on Amy’s list.) This was one of the first blogs to sum up a body of common knowledge regarding software, project manager, and software business practices. No one thing Joel Spolsky said was particularly astounding, but the general strand of good sense was highly valuable. Among other things, Spolsky has well-defended the idea that great developers need great offices, great tools, decent pay, and the right amount of work. Not much new stuff from Joel recently.
  • That’s really it.
  • There are some other bloggers that I’ve been avid for in the past, such as Jeremy Zawodny, Matt Raible, Phil Windley, James Gosling, Cameron Purdy, Eric Sink, and others, but it’s amazing how a lot of these folks have cooled off. Also, sometimes bloggers become quasi-famous, and then they think they can blog about whatever they want. But guess what, I was interested in their core competency. So they get unsubscribed.

Ruby and Rails

  • Amy and I both subscribe to Err the Blog, Rails Envy, Jamis Buck, Amy Hoy, Nuby on Rails, Riding Rails, and Loud Thinking. See Amy’s post to get those URLs.
  • Headius, Charles Nutter’s blog on JRuby.
  • Ruby Inside, the blog of Peter Cooper, author of Beginning Ruby.
  • Ola Bini’s blog; Ola is a developer at ThoughtWorks who contributes to JRuby. Ola seems to be one of the smartest bloggers out there. I learn something from every post.
  • O’Reilly Ruby. The best posts are by Greg Brown, who has talked to the Boston Ruby Group.
  • Rubylution - This one has a nice post on operator precedence that builds on what I warned you about regarding and, &&, &.

That’s enough for now. Let the group know if you find any interesting blogs that are relevant to the course.

Beginning Ruby - half price

Posted by john in Ruby No Comments »

If you’ve looked at the books page, you know that I like Peter Cooper’s Beginning Ruby.

Cooper reports that it can now be acquired for 1/2 the price.

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Login