ruby quiz question '*' on the initialize parameters

G

Gaston Garcia

------=_Part_9794_18828428.1125080159842
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Hi everyone:

On the ruby quiz called: Banned Words (#9), I'm looking at the class posted=
=20
there and I came up with something I had never seen before (probably becaus=
e=20
I'm really new to Ruby)

class LanguageFilter
def initialize( *banned_words )
@banned_words =3D banned_words.flatten.sort
@clean_calls =3D 0
end

end

I had never seen this:

def initialize(*banned_words)

what does that * do? Does it mean I can send as many parameters as I want?

Thanks to all.


--=20
-gaston
http://www.hermanobrother.com/blog/

------=_Part_9794_18828428.1125080159842--
 
J

Joe Van Dyk

Hi everyone:
=20
On the ruby quiz called: Banned Words (#9), I'm looking at the class post= ed
there and I came up with something I had never seen before (probably beca= use
I'm really new to Ruby)
=20
class LanguageFilter
def initialize( *banned_words )
@banned_words =3D banned_words.flatten.sort
@clean_calls =3D 0
end
=20
end
=20
I had never seen this:
=20
def initialize(*banned_words)
=20
what does that * do? Does it mean I can send as many parameters as I want=
?


Yes. And it sticks them into an array.

-bash-2.05b$ cat a.rb
def foo *args
p args
end
foo 1, 2, 3, 4, 5, [6, 7]



-bash-2.05b$ ruby a.rb
[1, 2, 3, 4, 5, [6, 7]]

Hope that makes sense.
 
D

David A. Black

Hi --

Hi everyone:

On the ruby quiz called: Banned Words (#9), I'm looking at the class posted
there and I came up with something I had never seen before (probably because
I'm really new to Ruby)

class LanguageFilter
def initialize( *banned_words )
@banned_words = banned_words.flatten.sort
@clean_calls = 0
end

end

I had never seen this:

def initialize(*banned_words)

what does that * do? Does it mean I can send as many parameters as I want?

Yes -- it creates an array out of whatever comes in, zero or more of
them. You can also put it at the end of a composite arglist:

class C
def initialize(a,b,*c)
end

C.new(1,2,3,4,5) # a == 1, b == 2, c == [3,4,5]


David
 
G

Gaston Garcia

------=_Part_9826_16593048.1125080649483
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Thanks james, that's a beautiful thing that does!!!=20

-gaston

=20
On Aug 26, 2005, at 1:16 PM, Gaston Garcia wrote:
=20
=20
You guessed it. It will collect all passed parameters into an Array
object and store that in the variable banned_words.
=20
Welcome to Ruby.
=20
James Edward Gray II
=20
=20


--=20
-gaston

------=_Part_9826_16593048.1125080649483--
 
J

James Edward Gray II

Thanks james, that's a beautiful thing that does!!!

It also works in reverse. Watch this:

irb(main):001:0> def var_args( *args )
irb(main):002:1> fixed_args(*args) # expanded back out
irb(main):003:1> end
=> nil
irb(main):004:0> def fixed_args( one, two, three )
irb(main):005:1> p three
irb(main):006:1> end
=> nil
irb(main):007:0> var_args 1, 2, 3
3
=> nil

James Edward Gray II
 
J

James Edward Gray II

well, now I got lost with that reverse example. but I did get the
first one.

It just means that if you have an Array of arguments, you can expand
it out into the individual members when passing as the final argument
in a method. See if this clears it up:

irb(main):001:0> def show( one, two = nil, three = nil )
irb(main):002:1> puts "one = #{one.inspect}"
irb(main):003:1> puts "two = #{two.inspect}"
irb(main):004:1> puts "three = #{three.inspect}"
irb(main):005:1> end
=> nil
irb(main):006:0> arr = [1, 2, 3]
=> [1, 2, 3]
irb(main):007:0> show arr
one = [1, 2, 3]
two = nil
three = nil
=> nil
irb(main):008:0> show *arr
one = 1
two = 2
three = 3
=> nil

In the first call, I pass a single argument (an Array of numbers).
In the second call, I pass the numbers themselves (three arguments).

Hope that helps.

James Edward Gray II
 
J

Joe Van Dyk

well, now I got lost with that reverse example. but I did get the first o= ne.
=20
thanks! : )

% cat a.rb
def foo x, y, z
puts "x: #{ x }, y: #{ y }, z: #{ z }"
end

my_array =3D [1, 2, 3]
foo *my_array


% ruby a.rb
x: 1, y: 2, z: 3
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top