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?
You guessed it. It will collect all passed parameters into an Array object and store that in the variable banned_words.
Welcome to Ruby.
James Edward Gray II
···
On Aug 26, 2005, at 1:16 PM, Gaston Garcia wrote:
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?
-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.
···
On 8/26/05, Gaston Garcia <gaston.garcia@gmail.com> wrote:
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?
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:
Thanks james, that's a beautiful thing that does!!!
-gaston
···
On 8/26/05, James Edward Gray II <james@grayproductions.net> wrote:
On Aug 26, 2005, at 1:16 PM, Gaston Garcia wrote:
> 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?
You guessed it. It will collect all passed parameters into an Array
object and store that in the variable banned_words.
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
···
On Aug 26, 2005, at 1:39 PM, Gaston Garcia wrote:
well, now I got lost with that reverse example. but I did get the first one.
well, now I got lost with that reverse example. but I did get the first one.
thanks! : )
% cat a.rb
def foo x, y, z
puts "x: #{ x }, y: #{ y }, z: #{ z }"
end
my_array = [1, 2, 3]
foo *my_array
% ruby a.rb
x: 1, y: 2, z: 3
···
On 8/26/05, Gaston Garcia <gaston.garcia@gmail.com> wrote:
On 8/26/05, James Edward Gray II <james@grayproductions.net> wrote:
>
> On Aug 26, 2005, at 1:24 PM, Gaston Garcia wrote:
>
> > 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
>
>
>