Hello again ruby community! I just learned how to add two arrays(I know, i know). My program looked like this array1=[1,2,3] array2=[4,5,6] array_sum=array1+array2 I thought pretty simple stuff, they are combined. However, now i am looking to define that code as a method and I do not understand how to create the correct number of arguments, so when the method is called back it gives me my array_sum. I have been trying many different variations, but it has now come down to I am not sure if my defining is wrong or my code in the method is incorrect. I want to say it is something such as def please_work() array1=[1,2,3] array2=[4,5,6] array_sum=array1+array2 end puts please_work(array_sum) i ended up fiddling around and getting it to output [1,2,3,4,5,6], but my syntex said i did not have the correct number of arguments. Also, sometimes i get array_sum is not a defined variable. If I am defining it in the method, shouldn''t it be defined if i call the method again? thanks taking a look at my question alex -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/bc0aa129b0271b8f7159b64b5d0f6fd8%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.
def array_sum(array1 = [], array2 = []) array1 + array2 end array_sum([1,2,3], [4,5,6]) => [1, 2, 3, 4, 5, 6] -- Dheeraj Kumar On Friday 30 August 2013 at 2:01 AM, Alex Froelich wrote:> Hello again ruby community! > > I just learned how to add two arrays(I know, i know). > > My program looked like this > > array1=[1,2,3] > array2=[4,5,6] > array_sum=array1+array2 > > I thought pretty simple stuff, they are combined. However, now i am > looking to define that code as a method and I do not understand how to > create the correct number of arguments, so when the method is called > back it gives me my array_sum. > > I have been trying many different variations, but it has now come down > to I am not sure if my defining is wrong or my code in the method is > incorrect. > > I want to say it is something such as > > def please_work() > array1=[1,2,3] > array2=[4,5,6] > array_sum=array1+array2 > end > puts please_work(array_sum) > > i ended up fiddling around and getting it to output [1,2,3,4,5,6], but > my syntex said i did not have the correct number of arguments. Also, > sometimes i get array_sum is not a defined variable. If I am defining it > in the method, shouldn''t it be defined if i call the method again? > > thanks taking a look at my question > alex > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org (mailto:rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org). > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org (mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org). > To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/bc0aa129b0271b8f7159b64b5d0f6fd8%40ruby-forum.com. > For more options, visit https://groups.google.com/groups/opt_out. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/8C5A4B6AC18E4B879C13839A3B5582B4%40gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
On 29 August 2013 21:40, Dheeraj Kumar <a.dheeraj.kumar-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> def array_sum(array1 = [], array2 = []) > array1 + array2 > end > > array_sum([1,2,3], [4,5,6]) > => [1, 2, 3, 4, 5, 6]Also I suggest the OP works through a good Ruby primer. Colin> > -- > Dheeraj Kumar > > On Friday 30 August 2013 at 2:01 AM, Alex Froelich wrote: > > Hello again ruby community! > > I just learned how to add two arrays(I know, i know). > > My program looked like this > > array1=[1,2,3] > array2=[4,5,6] > array_sum=array1+array2 > > I thought pretty simple stuff, they are combined. However, now i am > looking to define that code as a method and I do not understand how to > create the correct number of arguments, so when the method is called > back it gives me my array_sum. > > I have been trying many different variations, but it has now come down > to I am not sure if my defining is wrong or my code in the method is > incorrect. > > I want to say it is something such as > > def please_work() > array1=[1,2,3] > array2=[4,5,6] > array_sum=array1+array2 > end > puts please_work(array_sum) > > i ended up fiddling around and getting it to output [1,2,3,4,5,6], but > my syntex said i did not have the correct number of arguments. Also, > sometimes i get array_sum is not a defined variable. If I am defining it > in the method, shouldn''t it be defined if i call the method again? > > thanks taking a look at my question > alex > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To view this discussion on the web visit > https://groups.google.com/d/msgid/rubyonrails-talk/bc0aa129b0271b8f7159b64b5d0f6fd8%40ruby-forum.com. > For more options, visit https://groups.google.com/groups/opt_out. > > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To view this discussion on the web visit > https://groups.google.com/d/msgid/rubyonrails-talk/8C5A4B6AC18E4B879C13839A3B5582B4%40gmail.com. > > For more options, visit https://groups.google.com/groups/opt_out.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLtonPh-Jfah598uFUHXbtMWTMqmFY3JsRhjqzUGnE-kKg%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
Colin is right. You should try reading a book, like this one: http://pragprog.com/book/ruby4/programming-ruby-1-9-2-0 -- Dheeraj Kumar On Friday 30 August 2013 at 2:12 AM, Colin Law wrote:> On 29 August 2013 21:40, Dheeraj Kumar <a.dheeraj.kumar-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org (mailto:a.dheeraj.kumar-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org)> wrote: > > def array_sum(array1 = [], array2 = []) > > array1 + array2 > > end > > > > array_sum([1,2,3], [4,5,6]) > > => [1, 2, 3, 4, 5, 6] > > > > > Also I suggest the OP works through a good Ruby primer. > > Colin > > > > > -- > > Dheeraj Kumar > > > > On Friday 30 August 2013 at 2:01 AM, Alex Froelich wrote: > > > > Hello again ruby community! > > > > I just learned how to add two arrays(I know, i know). > > > > My program looked like this > > > > array1=[1,2,3] > > array2=[4,5,6] > > array_sum=array1+array2 > > > > I thought pretty simple stuff, they are combined. However, now i am > > looking to define that code as a method and I do not understand how to > > create the correct number of arguments, so when the method is called > > back it gives me my array_sum. > > > > I have been trying many different variations, but it has now come down > > to I am not sure if my defining is wrong or my code in the method is > > incorrect. > > > > I want to say it is something such as > > > > def please_work() > > array1=[1,2,3] > > array2=[4,5,6] > > array_sum=array1+array2 > > end > > puts please_work(array_sum) > > > > i ended up fiddling around and getting it to output [1,2,3,4,5,6], but > > my syntex said i did not have the correct number of arguments. Also, > > sometimes i get array_sum is not a defined variable. If I am defining it > > in the method, shouldn''t it be defined if i call the method again? > > > > thanks taking a look at my question > > alex > > > > -- > > Posted via http://www.ruby-forum.com/. > > > > -- > > You received this message because you are subscribed to the Google Groups > > "Ruby on Rails: Talk" group. > > To unsubscribe from this group and stop receiving emails from it, send an > > email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org (mailto:rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org). > > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org (mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org). > > To view this discussion on the web visit > > https://groups.google.com/d/msgid/rubyonrails-talk/bc0aa129b0271b8f7159b64b5d0f6fd8%40ruby-forum.com. > > For more options, visit https://groups.google.com/groups/opt_out. > > > > > > -- > > You received this message because you are subscribed to the Google Groups > > "Ruby on Rails: Talk" group. > > To unsubscribe from this group and stop receiving emails from it, send an > > email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org (mailto:rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org). > > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org (mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org). > > To view this discussion on the web visit > > https://groups.google.com/d/msgid/rubyonrails-talk/8C5A4B6AC18E4B879C13839A3B5582B4%40gmail.com. > > > > For more options, visit https://groups.google.com/groups/opt_out. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org (mailto:rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org). > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org (mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org). > To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLtonPh-Jfah598uFUHXbtMWTMqmFY3JsRhjqzUGnE-kKg%40mail.gmail.com. > For more options, visit https://groups.google.com/groups/opt_out. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/B4B4251B48AB4C1CB48A6FA89BE382CE%40gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
Dheeraj Kumar wrote in post #1119998:> Colin is right. You should try reading a book, like this one: > http://pragprog.com/book/ruby4/programming-ruby-1-9-2-0 > > -- > Dheeraj KumarHi Colin and Dheeraj, First off thanks for your help. It is actually quite funny, I ordered that exact book a few days ago :). I also worked through Chris Pine''s book, which was very nice. I always think i get the ideas, practice the items that they show, but when it comes to solving problems I freeze up. Guess it will come with a ton more practice and reading. thanks for taking the time to help Alex -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/663ea4184ef203b628be2add1681933b%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.
Dheeraj Kumar wrote in post #1119995:> def array_sum(array1 = [], array2 = []) > array1 + array2 > end > > array_sum([1,2,3], [4,5,6]) > => [1, 2, 3, 4, 5, 6] > > > -- > Dheeraj KumarIs there a reason why when i would run a test it would say -1 arguments instead of 2? You had listed two (array1 = [], array2= []), I have never actually gotten - arguments before with the spec test. thanks! -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/0a873e3de591a2bfb3551dc08c212449%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.
Could you paste your test? -- Dheeraj Kumar On Friday 30 August 2013 at 2:37 AM, Alex Froelich wrote:> Dheeraj Kumar wrote in post #1119995: > > def array_sum(array1 = [], array2 = []) > > array1 + array2 > > end > > > > array_sum([1,2,3], [4,5,6]) > > => [1, 2, 3, 4, 5, 6] > > > > > > -- > > Dheeraj Kumar > > > > > Is there a reason why when i would run a test it would say -1 arguments > instead of 2? You had listed two (array1 = [], array2= []), I have never > actually gotten - arguments before with the spec test. > > thanks! > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org (mailto:rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org). > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org (mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org). > To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/0a873e3de591a2bfb3551dc08c212449%40ruby-forum.com. > For more options, visit https://groups.google.com/groups/opt_out. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/831C9BE2A23945999A263DFFFD4B704E%40gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
Dheeraj Kumar wrote in post #1120004:> Could you paste your test? > > -- > Dheeraj KumarThe code for the number of arguments and the part which is failing is it "requires two arguments" do method(:array_sum).arity.should eq 2 end Thanks Dheeraj. Quick question about the pick axe book. Have you given a version a read back in the day? I am just wondering how beginner friendly it is. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/7a5a96d399f82cfc7803630ce33503b4%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.
The arity should be -1, not 2. See the docs here: http://www.ruby-doc.org/core-1.9.3/Method.html#method-i-arity I read the pickaxe book about three years ago, when I first started with Ruby 1.9.2. It was quite beginner friendly, and very useful. I haven''t read more recent editions. -- Dheeraj Kumar On Friday 30 August 2013 at 3:32 AM, Alex Froelich wrote:> Dheeraj Kumar wrote in post #1120004: > > Could you paste your test? > > > > -- > > Dheeraj Kumar > > > > > The code for the number of arguments and the part which is failing is > > > it "requires two arguments" do > method(:array_sum).arity.should eq 2 > end > > > Thanks Dheeraj. Quick question about the pick axe book. Have you given > a version a read back in the day? I am just wondering how beginner > friendly it is. > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org (mailto:rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org). > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org (mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org). > To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/7a5a96d399f82cfc7803630ce33503b4%40ruby-forum.com. > For more options, visit https://groups.google.com/groups/opt_out. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CCE02DE6380149D1AD2EDED407E82B13%40gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
On Thursday, August 29, 2013 11:02:10 PM UTC+1, Ruby-Forum.com User wrote:> > > The code for the number of arguments and the part which is failing is > > > it "requires two arguments" do > method(:array_sum).arity.should eq 2 > end > > >Dheeraj''s code has optional arguments. In such circumstances arity will return a negative number (where -1 means that the method requires at least 0 arguments, -1 would mean a method that requires at least 1 argument etc...> Thanks Dheeraj. Quick question about the pick axe book. Have you given > a version a read back in the day? I am just wondering how beginner > friendly it is. >It was the first book on ruby I ever read Fred> > -- > Posted via http://www.ruby-forum.com/. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/819a7f9d-0c58-41aa-be1e-431872cd6fae%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Frederick Cheung wrote in post #1120015:> On Thursday, August 29, 2013 11:02:10 PM UTC+1, Ruby-Forum.com User > wrote: >> > Dheeraj''s code has optional arguments. In such circumstances arity will > return a negative number (where -1 means that the method requires at > least > 0 arguments, -1 would mean a method that requires at least 1 argument > etc... > > >> Thanks Dheeraj. Quick question about the pick axe book. Have you given >> a version a read back in the day? I am just wondering how beginner >> friendly it is. >> > > It was the first book on ruby I ever read > > FredThanks! Look forward to reading it. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/9763ccef1080603be23d7b0164e2bb86%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.