Hello, i need some help here, when i run the following code i get the
error below. Why is that, according to the textbook it should work, also
looked it up in the cookbook, no luck there too. Thanks in advance.
Hello, i need some help here, when i run the following code i get the
error below. Why is that, according to the textbook it should work, also
looked it up in the cookbook, no luck there too. Thanks in advance.
Hello, i need some help here, when i run the following code i get the
error below. Why is that, according to the textbook it should work, also
looked it up in the cookbook, no luck there too. Thanks in advance.
def split_apart(first, *splat, last)
What Robert is saying is that is valid syntax for ruby 1.9, but not for
ruby 1.8.
I don't know what "the textbook" and "the cookbook" are that you refer
to, but they were probably written for 1.9.
ruby 1.9 is a substantially different language to ruby 1.8, which you
probably wouldn't expect from the "minor" version bump.
I tried putting end after the last brace but didn't work. I am assuming
there is more to that ^ than just for illustrative purposes. Could you
explain to me why my code is not working? THanks!
Thanks guys for all the input as well as the timely responses! I have to
upgrade my Ruby version then, i thought i had the latest version
installed but seems not.
Hello, i need some help here, when i run the following code i get the
error below. Why is that, according to the textbook it should work, also
looked it up in the cookbook, no luck there too. Thanks in advance.
I tried putting end after the last brace but didn't work. I am assuming there is more to that ^ than just for illustrative purposes. Could you explain to me why my code is not working? THanks!
In Ruby, formal parameters have a particular order:
You cannot have a regular required parameter after an optional parameter (denoted by *). The only thing you can have is a block parameter (denoted by &) or nothing at all.
Robert is saying that it works in 1.9.1 (see how it says Syntax OK), but not
1.8.7 (where it pulls up the same error you had)
For 1.9, you can do something like this
def split_apart( first , *splat )
raise ArgumentError.new('wrong number of arguments (1 for 2)') if
splat.size.zero?
last = splat.pop
puts "first: #{first.inspect}, splat: #{splat.inspect}, last:
#{last.inspect}"
end
Though I probably wouldn't bother with the error myself, unless writing code
for other people.
This is similar to how Rails' find method works, it checks the last argument
to see if it is a hash, then if it is, it pops it off (look at their example
in the comments)
···
On Tue, May 4, 2010 at 4:31 AM, Ruby Knight <kerwinfranks@yahoo.co.uk>wrote:
>
> def s(a,*b,c) end
> ^
> 11:16:32 test_2$
>
> Cheers
>
> robert
Hi Robert,
I tried putting end after the last brace but didn't work. I am assuming
there is more to that ^ than just for illustrative purposes. Could you
explain to me why my code is not working? THanks!
On Tue, May 4, 2010 at 5:35 AM, Ruby Knight <kerwinfranks@yahoo.co.uk>wrote:
Thanks guys for all the input as well as the timely responses! I have to
upgrade my Ruby version then, i thought i had the latest version
installed but seems not.