From: Robert Klemme Date: 2010-08-29T18:35:12+09:00 Subject: Re: initialize instance variable problem On 28.08.2010 18:17, Cameron Vessey wrote: > Why does this return nil for @city? > > class Url > def initialize(city,quary,catagory,minask,maxask) > @city = city > @quary = quary > @cat = catagory > @min = minask > @max = maxask > end > > puts @city > > end > a = Url.new('spokane',"tires","pts","0","1000") > a Others have identified the basic issue (your puts statement accesses a member of the class and not of the instance) but I'd like to add a few other remarks. First, the name Url might cause confusion because someone might think it is a generic URL handling class (like class URI for example). What you basically should do is add attribute accessors for your instance variables, e.g. class Url attr :city end Then you can do a = Url.new ... puts a.city But: you can make your life much simpler by resorting to Struct: CityUrl = Struct.new :city, :quary, :catagory, :minask, :maxask This gives you a proper initialize, attribute accessors and a few more things for free. irb(main):008:0> a = CityUrl.new('spokane',"tires","pts","0","1000") => # irb(main):009:0> a.city => "spokane" Btw, from the naming and values of "minask" and "maxask" it seems these should be rather used with integer values. Even if you read them as strings from some sort of file your application logic will become much easier if you convert those strings on reading into ints and then go from there. Otherwise you will have to convert them all the time you want to use them which is inefficient, error prone (you might forget a location) and tedious to change if you need to at one point. Also, you depend on the format of the single source which might make it difficult to pull the data from other sources later. Basically it's best to always do something like external representation -> import (load) converts into internal representation -> work with that internal representation throughout the application -> export (store) converts to external representation Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/