tags:

views:

24

answers:

4
#!/usr/bin/ruby

@data = { :student => { :id => '123477'} } 

 class Student  
  end 
 s = Student.new

def s.id
   @data[:student][:id]
  #how can I access the @data variable here I tried @data[:student][:id] doesnt work
  #I realize that data is outside of the scope of this method. However, is there any way!
end 

puts s.id
A: 

Should you be defining an instance variable (prefixed by "@") outside of a class definition?

Also, you can't define a method with a period in the name

RyanHennig
+1  A: 

First off, your id method actually has to go into the class.

You could try something like this:

@data = { :student => { :id => '123477'} }

class Student
  attr_accessor :id

  def initialize(student)
    self.id = student[:id]
  end
end

s = Student.new(@data[:student])
puts s.id
vonconrad
No. This is a singleton. Your solution works but I am trying to learn singleton methods. To see what I mean go to:http://www.contextualdevelopment.com/articles/2008/ruby-singleton
I'm glad you made that comment. If you pass the right `self` into your method you *can* get at `@data`. Interesting question. See my answer...
DigitalRoss
A: 

#!/usr/bin/ruby
@data = { :student => { :id => '123477', :first_name => 'Lazlo', :last_name =>'Fortunatus', :email=>'[email protected]' }, :contact_info => { :telephone=>'1 415 222-2222', :address => '123 Main St', :city =>'Beverly Hills', :state=>'California', :zip_code=>90210, :social_security_number =>'111-11-1111' } } 

 class Student  
    def initialize( data )
        @data = data
    end

    def get_id_override
        @data[:student][:id]
    end 

    def get_first_name_override
        @data[:student][:first_name]
    end 

    def get_last_name_override
        @data[:student][:last_name]
    end 
        def get_email_override
        @data[:student][:email]
    end 

    def get_telephone_override
        @data[:contact_info][:telephone]
    end 
        def get_city_override
        @data[:contact_info][:city]
    end 

    def get_state_override
        @data[:contact_info][:state]
    end 

    def get_zip_code_override
        @data[:contact_info][:zip_code]
    end 

    def get_social_security_number_override
        @data[:contact_info][:social_security_number]
    end 




end 

s = Student.new(@data)

def s.id
    get_id_override
end 
def s.first_name
    get_first_name_override
end 

def s.last_name
    get_last_name_override
end 

def s.email
    get_email_override
end 

def s.contact_info
    get_telephone_override
end 

def s.city
    get_city_override
end

def s.state
    get_state_override
end

def s.zipcode
    get_zip_code_override
end

def s.ssn
    get_social_security_number_override
end

puts s.id
puts s.first_name
puts s.last_name
puts s.email
puts s.contact_info
puts s.city
puts s.state
puts s.zipcode
puts s.ssn

Here is the answer after some work. Anyone has a better answer than mine let me know.

A: 

It can be done!


It didn't at first work because @data is an instance attribute of the top level object, so even though Student is derived from Object the attribute isn't in the new instance.

But you can pass self into s.id, and so then the only thing you need to add is an accessor for the data attribute.

However, that's slightly tricky because attr_reader et al are private class methods so you can't use them directly, and you can't (because it's private) just say self.class.attr_reader, you have to open up Object and do it...with these changes your program works...

@data = { :student => { :id => '123477'} } 

class Student
end 
s = Student.new

def s.id o
   o.data[:student][:id]
  #how can I access the @data variable here I tried @data[:student][:id] doesnt work
  #I realize that data is outside of the scope of this method. However, is there any way!
end 

class Object
  attr_reader :data
end

puts s.id self
DigitalRoss