[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Methods and :parameters

Pedro Del Gallego

2/26/2007 1:58:00 AM

Hi all,

I've a question about the :parameter notation.
I want to write a method that can hold several optional paramters

add_book :title=>"El quijote", :author=> "Miguel de Cervantes"
add_book :title=>"El quijote", :author=> "Miguel de Cervantes", :tag=>"novel"
add_book :title=>"El quijote"

and i had write the add_book mehod like this :

class Book
attr_writer :title, :author
end

def add_book (book )
puts "title : #{book.title} -- Author : #{book.author}"
end

but the interpreter said : ./bibliom.rb:7:in `add_book': undefined
method `title' for {:title=>"El quijote", :author=>"Miguel de
Cervantes"}:Hash (NoMethodError)

I also try to put a hash object in the incoming parameter, like that :

def add_book (book=[]) .... end

My question is how can i wirte a method with several optional
parameter and then acces they inside of the mehotd ?

Thanks


--
-------------------------------------
Pedro Del Gallego

Email : pedro.delgallego@gmail.com

6 Answers

Raj Sahae

2/26/2007 3:11:00 AM

0

Pedro Del Gallego wrote:
> I've a question about the :parameter notation.
> I want to write a method that can hold several optional paramters
>
> add_book :title=>"El quijote", :author=> "Miguel de Cervantes"
> add_book :title=>"El quijote", :author=> "Miguel de Cervantes",
> :tag=>"novel"
> add_book :title=>"El quijote"
To have a method with optional variables, assign them to nil in the
declaration. If they aren't included in the method call, they will be
set to nil.

def add_book(title, author = nil, tag = nil)
> but the interpreter said : ./bibliom.rb:7:in `add_book': undefined
> method `title' for {:title=>"El quijote", :author=>"Miguel de
> Cervantes"}:Hash (NoMethodError)
It gives you this error because you are using attr_writer, which is
equivalent to a def attr= method. In order to simply return a value,
you should use attr_reader, or in case you want both, attr_accessor.

I think what you want is along the lines of:

class Book
attr_accessor :title, :author, :tag

def initialize(title, author = nil, tag = nil)
self.title = title
self.author = author
self.tag = tag
end

def add_book
puts "title: #{self.title} -- Author: #{self.author}"
end
end

Hidetoshi NAGAI

2/26/2007 5:06:00 AM

0

Pedro Del Gallego

2/26/2007 1:03:00 PM

0

Wow ... thats exactly what i was looking for :). Can you explain me
how this override of the method_missing works? im a little bit lost
right after the when statament begin ...

> def method_missing(id, *args)
> prop = id.id2name
> case args.length
> when 1
> if prop[-1] == ?=
> self[prop[0..-2]] = args[0]
> args[0]
> else
> self[prop] = args[0]
> self
> end
> when 0
> self[prop]
> else
> super(id, *args)
> end
> end
>

Thanks again.

--
-------------------------------------
Pedro Del Gallego

Email : pedro.delgallego@gmail.com

Hidetoshi NAGAI

2/26/2007 2:13:00 PM

0

Ken Bloom

2/26/2007 3:09:00 PM

0

On Mon, 26 Feb 2007 10:57:59 +0900, Pedro Del Gallego wrote:

> Hi all,
>
> I've a question about the :parameter notation. I want to write a method
> that can hold several optional paramters
>
> add_book :title=>"El quijote", :author=> "Miguel de Cervantes"
> add_book :title=>"El quijote", :author=> "Miguel de Cervantes",
> :tag=>"novel" add_book :title=>"El quijote"
>
> and i had write the add_book mehod like this :
>
> class Book
> attr_writer :title, :author
> end
>
> def add_book (book )
> puts "title : #{book.title} -- Author : #{book.author}"
> end
>
> but the interpreter said : ./bibliom.rb:7:in `add_book': undefined
> method `title' for {:title=>"El quijote", :author=>"Miguel de
> Cervantes"}:Hash (NoMethodError)
>
> I also try to put a hash object in the incoming parameter, like that :
>
> def add_book (book=[]) .... end
>
> My question is how can i wirte a method with several optional parameter
> and then acces they inside of the mehotd ?


def add_book (book )
puts "title : #{book[:title]} -- Author : #{book[:author]}"
end

When using the named parameter idiom, all of the named parameters are
lumped together into a single hash which is passed as the last parameter.
There's no point in defining the Book class -- it won't be used by this
idiom.

If you want to access the parameters as members, then you can construct
an OpenStruct inside the method, like so

require 'ostruct'

def add_book book
book=OpenStruct.new(book)
puts "title : #{book.title} -- Author : #{book.author}"
end

but that's most likely overkill.

--
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu...

Pedro Del Gallego

2/26/2007 5:36:00 PM

0

> Are those OK?

Perfect :). Thanks a lot.


--
-------------------------------------
Pedro Del Gallego

Email : pedro.delgallego@gmail.com