Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #4735 > unrolled thread
| Started by | Andreas Lundgren <andreas.lundgren.x@gmail.com> |
|---|---|
| First post | 2011-05-18 06:44 -0700 |
| Last post | 2011-05-24 16:54 -0500 |
| Articles | 8 on this page of 28 — 6 participants |
Back to article view | Back to comp.lang.ruby
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
Page 2 of 2 — ← Prev page 1 [2]
| From | Robert Klemme <shortcutter@googlemail.com> |
|---|---|
| Date | 2011-05-24 10:20 -0500 |
| Message-ID | <BANLkTikFzxMyhxKv2z4nrD4y+rzubEqLjA@mail.gmail.com> |
| In reply to | #4989 |
On Tue, May 24, 2011 at 3:50 PM, Andreas Lundgren
<andreas.lundgren.x@gmail.com> wrote:
>> > First params typically assembles to this:
>> > x1.to_s + ', ' + x2.to_s + ', ' + x3.to_s + ', ' + x4.to_s
>>
>> That does not look good. If you have to encode numbers in variable
>> names, you probably rather want to use an Array for that or even
>> generate that directly:
>>
>> params = no_of_params.times.map {|i| "x#{i}"}.join(', ')
>
> your example generates code looks like this:
> params = x1, x2, x3, x4
> But with that I get an error (eval):3:in `+': can't convert Fixnum
> into String (TypeError)
Without the code you are executing that error is pretty useless.
It works for me:
irb(main):023:0> STUB_CALLS=false
(irb):23: warning: already initialized constant STUB_CALLS
=> false
irb(main):024:0> GET_VALUE "f", "m", "r", 4
def f(x0, x1, x2, x3)
oai = OA.instance
handle = oai.getWIN32OLEHandle()
handle.call(['MethodNameIn','Params'],['m', [x0, x1, x2, x3]]);
ret_val = handle.GetControlValue(r);
error = handle.GetControlValue('error out');
return oai.checkForError(error) ? 'NaN': ret_val
end
=> nil
irb(main):025:0> STUB_CALLS=true
(irb):25: warning: already initialized constant STUB_CALLS
=> true
irb(main):026:0> GET_VALUE "f", "m", "r", 4
def f(x0, x1, x2, x3)
printf("Calling m(x0, x1, x2, x3) with parameters <%p>\n", [x0, x1, x2, x3])
end
=> nil
irb(main):027:0>
(with my definition of GET_VALUE from earlier)
>> params_2 = "#{x1}, #{x2}, #{x3}, #{x4}"
> Yes, I could use this, but still I need to assembly this line since I
> dont know the number of input parameters at coding time. And I know
> that no input parameters contain any expressions that needs to be
> evaluated since it is an input from my generated function.
I find the solution with printf "%p" and the Array more elegant.
Cheers
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
[toc] | [prev] | [next] | [standalone]
| From | 7stud -- <bbxx789_05ss@yahoo.com> |
|---|---|
| Date | 2011-05-24 17:27 -0500 |
| Message-ID | <917a6bebd84192c90d0434fe9ec9e2a4@ruby-forum.com> |
| In reply to | #4964 |
Andreas Lundgren wrote in post #1000535:
> First params does not contain a simple string;
> please note the escaped string characters in the code
> that generates params. Kind of strings within strings
Yes, I noted that--see comment 6). What I failed to recognize was that
the line:
params = #{params}
was part of a multiline string.
> "1) Strings are mutable in ruby, so get rid of all those +'s." - I'm
> not sure that I understand this but it sounds interesting, what does
> it mean?
>
Every quoting mechanism creates a string and every + creates a new
combined string. So it's more efficient to use string interpolation:
i = 2
params = 'x1.to_s'
params = "#{params}, x#{i}.to_s"
p params
--output:--
"x1.to_s, x2.to_s"
In ruby not only can you push elements onto an array with the << method,
you can also push a string onto another string with the << method--which
alters the first string:
i = 2
params = 'x1.to_s'
params = "#{params}, x#{i}.to_s"
puts params
puts params.object_id
params << ', x3.to_s'
puts params
puts params.object_id
--output:--
x1.to_s, x2.to_s
77684510
x1.to_s, x2.to_s, x3.to_s
77684510
--
Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | 7stud -- <bbxx789_05ss@yahoo.com> |
|---|---|
| Date | 2011-05-24 17:53 -0500 |
| Message-ID | <a7767bf3bf24b12dca32299294f73970@ruby-forum.com> |
| In reply to | #5009 |
7stud -- wrote in post #1000754:
> Andreas Lundgren wrote in post #1000535:
>> First params does not contain a simple string;
>> please note the escaped string characters in the code
>> that generates params. Kind of strings within strings
>
> What I failed to recognize was that the line:
>
> params = #{params}
>
> was part of a multiline string.
>
>
>
>> "1) Strings are mutable in ruby, so get rid of all those +'s." - I'm
>> not sure that I understand this but it sounds interesting, what does
>> it mean?
>>
>
> Every quoting mechanism creates a string and every + creates a new
> combined string. So it's more efficient to use string interpolation:
>
> i = 2
> params = 'x1.to_s'
> params = "#{params}, x#{i}.to_s"
> p params
>
> --output:--
> "x1.to_s, x2.to_s"
>
>
> In ruby not only can you push elements onto an array with the << method,
> you can also use the << method to push a string onto another
> string--which
> alters the first string:
>
>
> i = 2
> params = 'x1.to_s'
> params = "#{params}, x#{i}.to_s"
>
> puts params
> puts params.object_id
>
> params << ', x3.to_s'
>
> puts params
> puts params.object_id
>
> --output:--
> x1.to_s, x2.to_s
> 77684510
> x1.to_s, x2.to_s, x3.to_s
> 77684510
So your loop here:
params = 'x1.to_s';
for i in 2..no_of_params do
arg_list = arg_list + ', x' + i.to_s;
params = params + ' + \', \' + x' + i.to_s + '.to_s';
end
params = params+';';
would simplify to:
num_params = 4
params = 'x1.to_s'
2.upto(num_params).each do |i|
params << ", x#{i}.to_s"
end
p params
--output:--
"x1.to_s, x2.to_s, x3.to_s, x4.to_s"
Also, compare that output to the output your loop produces:
"x1.to_s + ', ' + x2.to_s + ', ' + x3.to_s + ', ' + x4.to_s"
You would get many errors using that string as an argument list for a
method.
--
Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | Andreas Lundgren <andreas.lundgren.x@gmail.com> |
|---|---|
| Date | 2011-05-25 03:59 -0700 |
| Message-ID | <ed726629-f977-4281-81b6-776e22f4e580@y31g2000vbp.googlegroups.com> |
| In reply to | #5010 |
> --output:-- > "x1.to_s, x2.to_s, x3.to_s, x4.to_s" > > Also, compare that output to the output your loop produces: > > "x1.to_s + ', ' + x2.to_s + ', ' + x3.to_s + ', ' + x4.to_s" > > You would get many errors using that string as an argument list for a > method. I need this since parameters to win32ole are sent as a string with a comma separated list of arguments. That is to send in x1=1, x2='A' and x3=1.5 I do the call: handle.call(['MethodNameIn','Params'],['x1, x2, x3', '1, A, 1.5']); The receiver mechanism does an "explode" on "," and it's then up to the receiver to type cast the different elements to the expected types (integer, character, float in this example) BR, Andreas
[toc] | [prev] | [next] | [standalone]
| From | 7stud -- <bbxx789_05ss@yahoo.com> |
|---|---|
| Date | 2011-05-25 12:26 -0500 |
| Message-ID | <fdbf43fe23ee24877a4ce4b4a579258b@ruby-forum.com> |
| In reply to | #5027 |
Andreas Lundgren wrote in post #1000859: >> --output:-- >> "x1.to_s, x2.to_s, x3.to_s, x4.to_s" >> >> Also, compare that output to the output your loop produces: >> >> "x1.to_s + ', ' + x2.to_s + ', ' + x3.to_s + ', ' + x4.to_s" >> >> You would get many errors using that string as an argument list for a >> method. > > I need this since parameters to win32ole are sent as a string with a > comma separated list of arguments. That is to send in x1=1, x2='A' and > x3=1.5 I do the call: > handle.call(['MethodNameIn','Params'],['x1, x2, x3', '1, A, 1.5']); > If you need to send the string 'x1, x2, x3', then you need to create that string, not this garbage: "x1.to_s + ', ' + x2.to_s + ', ' + x3.to_s + ', ' + x4.to_s" Do you not see the difference between these two strings: "x1, x2, x3" "x1.to_s + ', ' + x2.to_s + ', ' + x3.to_s + ', ' + x4.to_s" Do you not see that those strings have different lengths? You seem to think that ruby is going to do some type of eval on the second string and you will end up with the first string, but ruby is not going to do that. The second string above is a *string*, it is not an *expression*. -- Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | 7stud -- <bbxx789_05ss@yahoo.com> |
|---|---|
| Date | 2011-05-25 17:12 -0500 |
| Message-ID | <146d6aab925787bdba331de6c8335faa@ruby-forum.com> |
| In reply to | #5027 |
Andreas Lundgren wrote in post #1000859: > How do you get from this function signature: > def GET_VALUE(f_name, method, return_format, no_of_params) to this: > handle.call(['MethodNameIn','Params'],['x1, x2, x3', '1, A, 1.5']); Where do 1, 'A', and 1.5 come from? -- Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | Andreas Lundgren <andreas.lundgren.x@gmail.com> |
|---|---|
| Date | 2011-05-27 04:48 -0700 |
| Message-ID | <c4f99854-9708-40a7-8246-5c11a06f3009@u26g2000vby.googlegroups.com> |
| In reply to | #5062 |
Hi! This will be my last post in this thread, since now I'm back explaining what was already described in the beginning. Thanks all for your good answers. If you just found this thread and are looking for answers about how to generate functions in Ruby, the begining of this thread is very informative. > How do you get from this function signature: > > def GET_VALUE(f_name, method, return_format, no_of_params) > to this: > > handle.call(['MethodNameIn','Params'],['x1, x2, x3', '1, A, 1.5']); This is described earlier in this thread, see my post from Mon, 23 May 2011 05:56:34 > Where do 1, 'A', and 1.5 come from? Thas in example of input from the control layer that calls the generated ruby glue function. BR, Andreas
[toc] | [prev] | [next] | [standalone]
| From | 7stud -- <bbxx789_05ss@yahoo.com> |
|---|---|
| Date | 2011-05-24 16:54 -0500 |
| Message-ID | <9185100131ec594e813413d3eff8fbcc@ruby-forum.com> |
| In reply to | #4735 |
Speaking of elegance, you can change this: class <<self;self;end.class_eval code to this: instance_eval(code) -- Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [standalone]
Page 2 of 2 — ← Prev page 1 [2]
Back to top | Article view | comp.lang.ruby
csiph-web