SunQuest
           Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesRuby Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today!
  #1  
Old August 16th, 2007, 06:55 PM
dustpyle_x3 dustpyle_x3 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2006
Posts: 82 dustpyle_x3 User rank is Corporal (100 - 500 Reputation Level)dustpyle_x3 User rank is Corporal (100 - 500 Reputation Level)dustpyle_x3 User rank is Corporal (100 - 500 Reputation Level)dustpyle_x3 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 3 h 48 m 14 sec
Reputation Power: 4
Making an object initialize to nil

I'm doing an assignment from the Learning Ruby tutorial, and I'm having a bit of a problem. I have a student class that holds a first name, a last name, and an age; pretty generic. Now I figure that should an empty string be given for one of the names, or a value less than zero for the age, then the object should be nil, since there is no apparent logical way to just substitute the invalid values with some generic value. Thing is, I'm not sure how to initialize an object to nil. Here's my failed attempt:

Student class:
Ruby Code:
Original - Ruby Code
    class Student     attr_reader :f_name     attr_reader :l_name     attr_reader :age         def initialize(f_name,l_name,age)         if f_name.empty? || f_name == nil || l_name.empty? || l_name == nil || age < 0             return nil         end         @f_name = f_name         @l_name = l_name         @age = age     end         def full_name         return f_name + ' ' + l_name     end end


TestStudent class:
Ruby Code:
Original - Ruby Code
    require 'test/unit' require 'student' class TestStudent < Test::Unit::TestCase     def setup         @obj1 = Student.new('Dustin','Pyle',17)         @obj2 = Student.new('','Shulz',12)         @obj3 = Student.new('Jamey','',17)         @obj4 = Student.new('Benny','Rutledge',0)     end         def test_first_name         assert_equal('Dustin',@obj1.f_name)         assert_nil(@obj2)     end         def test_last_name         assert_equal('Pyle',@obj1.l_name)         assert_nil(@obj3)     end         def test_age         assert_equal(17,@obj1.age)         assert_nil(@obj4)     end         def test_full_name         assert_equal('Dustin Pyle',@obj1.full_name)         assert_nil(@obj2)         assert_nil(@obj3)         assert_nil(@obj4)     end end


The test objects are obviously not initializing to nil since I always get failures. How do I go about this?

Reply With Quote
  #2  
Old August 17th, 2007, 06:42 AM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 577 L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 8 h 49 m 8 sec
Reputation Power: 67
Send a message via AIM to L7Sqr
new is used to create an object, not to assign it a value. If you look at what Object.new returns it is not a 'value' as you suspect:
Code:
irb(main):002:0> class Test
irb(main):003:1> def initialize(val)
irb(main):004:2> nil if val < 0
irb(main):005:2> val
irb(main):006:2> end
irb(main):007:1> end
=> nil
irb(main):008:0> Test.new(42)
=> #<Test:0xb7f41a84>
irb(main):009:0>
So you have a couple of options, you can make a wrapper to the class and do your checks there
Code:
def wrapper(fname,lname,age)
   nil || Student.new(...) if not [fname,lname].include? nil and age > 0
end
Or a less elegant way is to have a valid 'flag' within the Student class itself
Code:
class Student
   def initialize(...)
      @valid = true if ...
      # ...
   end
end
__________________
-- I'll provide you with reference points; if they dont work, refer to something else.

If you process text, this might make your life a little easier.

Reply With Quote
  #3  
Old August 17th, 2007, 03:56 PM
dustpyle_x3 dustpyle_x3 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2006
Posts: 82 dustpyle_x3 User rank is Corporal (100 - 500 Reputation Level)dustpyle_x3 User rank is Corporal (100 - 500 Reputation Level)dustpyle_x3 User rank is Corporal (100 - 500 Reputation Level)dustpyle_x3 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 3 h 48 m 14 sec
Reputation Power: 4
Ah, I see. Thanks for the help.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > Making an object initialize to nil


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway