Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #4966
| From | Brian Candler <b.candler@pobox.com> |
|---|---|
| Newsgroups | comp.lang.ruby |
| Subject | Re: Generating Functions in Ruby |
| Date | 2011-05-24 03:12 -0500 |
| Organization | Service de news de lacave.net |
| Message-ID | <385d62a492942fc7a357c795049cbd57@ruby-forum.com> (permalink) |
| References | <5e21af14-befb-406e-b1c3-6758fe1527ab@s14g2000vbi.googlegroups.com> <dc2df7d480159878c42c65807d568cd8@ruby-forum.com> <151562ef-aebd-4999-8b7b-816c21a7c6df@m40g2000vbt.googlegroups.com> <71dec76a-e0c1-4a7e-b3e6-a397ca4be291@f9g2000vbz.googlegroups.com> |
Andreas Lundgren wrote in post #1000537:
> The line
> params_2 = #{params};
> extends to:
> params_2 = x1.to_s + ', ' + x2.to_s + ', ' + x3.to_s + ', ' + x4.to_s
Or you could do it like this:
params_2 = "#{x1}, #{x2}, #{x3}, #{x4}"
#{} interpolates the value of an expression into a string and calls to_s
automatically.
A slightly more DRY version:
params_2 = [x1, x2, x3, x4].join(", ")
Again, to_s is called automatically in this case.
Finally, if you used the *args syntax to collect a variable number of
args into an array, then it would become
params_2 = args.join(", ")
or even just: handle.call(['MethodNameIn','Params'],[method]+args)
> Output from the function when STUB_CALLS=1:
Tiny suggestion: STUB_CALLS = false / true would be clearer, because
then you could say
do_something if STUB_CALLS
rather than
do_something if STUB_CALLS != 0
A global variable ($stub_calls) would arguably be preferable, since you
can change it at runtime without generating a warning. And better again
would be an instance variable of a class or module, because then it
lives in its own namespace.
module MyStuff
@stub_calls = false # set the default
def self.stub_calls
@stub_calls
end
def self.stub_calls=(x)
@stub_calls = x
end
end
puts MyStuff.stub_calls
MyStuff.stub_calls = true
puts MyStuff.stub_calls
Another thing to look at is using heredoc, as it means you don't have to
worry about quoting double-quotes:
class <<self;self;end.class_eval <<EOS
def #{f_name}(#{arg_list})
puts "Hello world"
end
EOS
Anyway, these are just tiny style suggestions. Having working code is
worth far more :-)
--
Posted via http://www.ruby-forum.com/.
Back to comp.lang.ruby | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Generating Functions in Ruby Andreas Lundgren <andreas.lundgren.x@gmail.com> - 2011-05-18 06:44 -0700
Re: Generating Functions in Ruby Steve Klabnik <steve@steveklabnik.com> - 2011-05-18 09:34 -0500
Re: Generating Functions in Ruby Robert Klemme <shortcutter@googlemail.com> - 2011-05-18 09:54 -0500
Re: Generating Functions in Ruby 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-18 14:44 -0500
Re: Generating Functions in Ruby 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-18 20:55 -0500
Re: Generating Functions in Ruby Robert Klemme <shortcutter@googlemail.com> - 2011-05-19 08:31 +0200
Re: Generating Functions in Ruby 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-19 13:00 -0500
Re: Generating Functions in Ruby Robert Klemme <shortcutter@googlemail.com> - 2011-05-20 01:28 -0500
Re: Generating Functions in Ruby Andreas Lundgren <andreas.lundgren.x@gmail.com> - 2011-05-23 05:54 -0700
Re: Generating Functions in Ruby 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-23 16:25 -0500
Re: Generating Functions in Ruby Thomas Preymesser <thopre@gmail.com> - 2011-05-19 04:35 -0500
Re: Generating Functions in Ruby Brian Candler <b.candler@pobox.com> - 2011-05-19 10:06 -0500
Re: Generating Functions in Ruby 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-23 16:09 -0500
Re: Generating Functions in Ruby 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-23 20:51 -0500
Re: Generating Functions in Ruby 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-23 21:07 -0500
Re: Generating Functions in Ruby Andreas Lundgren <andreas.lundgren.x@gmail.com> - 2011-05-24 00:10 -0700
Re: Generating Functions in Ruby Andreas Lundgren <andreas.lundgren.x@gmail.com> - 2011-05-24 00:24 -0700
Re: Generating Functions in Ruby Brian Candler <b.candler@pobox.com> - 2011-05-24 03:12 -0500
Re: Generating Functions in Ruby Robert Klemme <shortcutter@googlemail.com> - 2011-05-24 03:39 -0500
Re: Generating Functions in Ruby Andreas Lundgren <andreas.lundgren.x@gmail.com> - 2011-05-24 06:46 -0700
Re: Generating Functions in Ruby Robert Klemme <shortcutter@googlemail.com> - 2011-05-24 10:20 -0500
Re: Generating Functions in Ruby 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-24 17:27 -0500
Re: Generating Functions in Ruby 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-24 17:53 -0500
Re: Generating Functions in Ruby Andreas Lundgren <andreas.lundgren.x@gmail.com> - 2011-05-25 03:59 -0700
Re: Generating Functions in Ruby 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-25 12:26 -0500
Re: Generating Functions in Ruby 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-25 17:12 -0500
Re: Generating Functions in Ruby Andreas Lundgren <andreas.lundgren.x@gmail.com> - 2011-05-27 04:48 -0700
Re: Generating Functions in Ruby 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-24 16:54 -0500
csiph-web