On Nov 30, 2007, at 1:54 PM, Nathan Viswa wrote:

> Need help to understand how the marked <<< def works. Thanks
>
> class Person
>  def initialize(lname, fname)
>    @lname = lname
>    @fname = fname
>  end
>
>  def lname
>    return @lname
>  end
>
>  def fname
>    return @fname
>  end
>
>  def lname=(myarg)          # see = <<<
>    @lname = myarg
>  end
>
>  def fname=(myarg)
>    @fname = myarg
>  end
> end
>
> steve = Person.new("Litt", "Stove")
> print "My name is ", steve.fname, " ", steve.lname, ".\n"
> steve.fname = "Steve"
> print "My name is ", steve.fname, " ", steve.lname, ".\n"
>
> steve = Person.new("Litt", "Stove")
>
> print "My name is ", steve.fname, " ", steve.lname, ".\n"
> steve.fname = "Steve"
> print "My name is ", steve.fname, " ", steve.lname, ".\n"
>
> #steve.fname("Steve") # err

The name of the method includes the = and the form:

	steve.fname = "Steve"

is just syntactic sugar for:

	steve.fname=("Steve")

> #steve.lname("Nathan") # err  ????

the lname method doesn't take any arguments.  def lname

> steve.lname() #
> print "My name is ", steve.fname, " ", steve.lname, ".\n"
>
> steve.lname = "Nat" #
> print "My name is ", steve.fname, " ", steve.lname, ".\n"
>
> steve.lname() #
> print "My name is ", steve.fname, " ", steve.lname, ".\n"
>
>
> I:\RubyNV>zzz-tut13-class.rb
> My name is Stove Litt.
> My name is Steve Litt.
> My name is Steve Litt.
> My name is Steve Nat.
> My name is Steve Nat.
>
> I:\RubyNV>
>
> XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
>
> if i use it stand alone it does not work!!
>
> class Person
>  def initialize(lname)
>    @lname = lname
>  end

   def lname
     @lname
   end

>  def lname=(myarg)      # see = <<<
>    @lname = myarg
>       end
>
> end
>
> steve = Person.new("Viswa")
>
> #puts steve.lname, 'x1' # err undefined methid lname
> #puts steve.lname(), 'x2' # err as above
> #puts steve.lname("nv"), 'x2' # err as above
>
> #steve.lname = "Nat" #
>
> #puts steve.lname, 'x2' #  err undefined methid lname
> #puts steve.lname(), 'x3' # # err undefined methid lname
>
> #steve.lname = "Nat" #
> #print "My name is ", steve.lname, ".\n" #err
>
> #steve.lname() # err
> #print "My name is ", steve.lname, ".\n"# err
> --  
> Posted via http://www.ruby-forum.com/.



You should get used to the shortcut to standard accessors.  Rather  
than building

def myname
   @myname
end

def myname=(arg)
   @myname = arg
end

You can just add this to your class:

attr_accessor :myname

In your case, the first class could be written as:

class Person
   attr_accessor :lname, :fname

   def initialize(lname, fname)
     @lname = lname
     @fname = fname
   end
end

-Rob

Rob Biedenharn		http://agileconsultingllc.com
Rob / AgileConsultingLLC.com