Entries RSS Comments RSS

Archive for September, 2009

Ruby Scope Confusion

Friday, September 18th, 2009

I ran into some confusion when messing around with a sample program I created that creates new instances of a class Car and names the car. I wanted to store my list of “funnynames” in an array and have my naming method grab a random name from the array via my “name” method which would be “initialized” (using the initialize method) upon creation of each new instance of the Car class. Blahblahblah. So my problem: class variables work for me, but instance variables don’t! I couldn’t find much on instance variables, because everything seemed to start talking about instance methods once they mentioned instance variables. It seemed like everyone was reluctant to point out that INSTANCE VARIABLES ARE INVISIBLE!?!?! outside of the class. You can’t access them outside of the class unless you use an accessor method (easiest) or other method.  If I’m wrong here, someone correct me. But to my knowledge, that’s how it works. However, if you store the data you’d like to access in a class variable, it works! Unfortunately. Since it’s so easy, but a bad idea, from what I hear.  Inheritance happens. Right? :)

And to think… these three ways to do the same thing in Ruby (minus side effects in the future if I were to use the class variable) aren’t even the only ways to do this.

It’s interesting for me that I discovered this basically on my own when developing a simple practice program. Wee! :)

class Car
  def name
    @funnynames=%w[bob judy mabel wiener]
    puts "Your car's name is #{@funnynames[rand(4)]}!"
  end
end

#defining array within method 

class Car
  @@funnynames=%w[bob judy mabel wiener]
  def name
    puts "Your car's name is #{@@funnynames[rand(4)]}!"
  end
end

#array as a class variable (thus I'm able to access it)

class Car
  @funnynames=%w[bob judy mabel wiener]
  class << self  #makes the accessor method relevant to the class rather than a single instance
  attr_accessor :funnynames
  def name
    puts "Your car's name is #{Car.funnynames[rand(4)]}!"
  end
end

# Voila! More on this later as I continue to learn the in's and out's

Currently Computing: Revisiting my text analyzer with the use of ARGV[] to analyze files via the command line rather than hardcoding in the file to access within the program. SO cool! Then back to more OO sample programs. Rails will happen. I’m just busy nerding out on Ruby itself for the time being. :)

What is Ruby without some rails?

Wednesday, September 16th, 2009

Why the lucky stiff vanished off the face of the earth by the way…  Grrr…

I’ve been struggling lately with that book of mine. While it is great reading, I feel like I’m getting a bit stuck. I want to move on to practical applications of Ruby. I thought Shoes might be my answer (I [allegedly] can develop desktop applications with Ruby!) Nope. _Why took that all with him when he vanished. (Along with Hackety Hack! Which I LOVED and which used Shoes, too!) Ughhh. (Note: I have since discovered after posting this that many people are still working on his projects, and many are available via github.) There are ways to use Ruby “practically” (or what connotates practically in my head anyways) via various GUIs, but the fact is that Ruby’s most popular and practical application is Ruby on Rails– via the web. That’s just how it is.I’m not mad about it. I just need a new course of action.

So, as of last night, I’ve started reading Ruby tutorials all over the web that prepare one for use of RoR. I now know the most important features of Ruby that help one with Rails implementations. Of course, these are all things I learned previously via my Ruby book, but it’s nice to know which items I will use often and which I can discard as “unimportant” temporarily. I foresee my difficulty with Rails being the structure itself, particularly the MVC backbone. I don’t have a lot of experience with the web, or with programming even. I have limited experience with PHP, CSS, and HTML. But I’m ready to fuse my skills together and see what happens. I’m lucky to have help just a few feet away, too. But I try not to bug him too much. :)

I have not yet created my first Rails application. I’ll keep you all posted. I can tell you that my first idea for a “project” is as ridiculous as it is cute. I am truly a female.  Let’s see how this goes though. Honestly, I’m confident this escapade will be interesting. At very least, it will be worth giggling over. :x

P.S. – Thank you to Alan for explaining some of the more complex features and implementations of OO to me over coffee and toast. I love you.