param-vars

Hi

What is the name of |x| - Vars ( a.map{|x| ... )

thanks

Opti

AND is it (somehow!) possible to use them in a pre-defined lambda
(like a local-var, without params for the lamba)?

thank you

Opti

···

Am 23.08.21 um 18:31 schrieb Die Optimisten:

Hi

What is the name of |x| - Vars ( a.map{|x| ... )

thanks

Opti

Hi

What is the name of |x| - Vars ( a.map{|x| ... )

thanks

Opti

AND is it (somehow!) possible to use them in a pre-defined lambda
(like a local-var, without params for the lamba)?

Example: myvar=99; lam->{ p myvar }; [1,2,3].map{|myvar| lam }

···

Am 23.08.21 um 18:35 schrieb Die Optimisten:

Am 23.08.21 um 18:31 schrieb Die Optimisten:

thank you

Opt

Do you mean block arguments?

Best Regards,
Mohit.
2021-8-24 | 12:52 am.

···

On 2021-8-24 12:31 am, Die Optimisten wrote:

Hi

What is the name of |x| - Vars ( a.map{|x| ... )

Hi

What is the name of |x| - Vars ( a.map{|x| ... )

thanks

Opti

AND is it (somehow!) possible to use them in a pre-defined lambda
(like a local-var, without params for the lamba)?

Example: myvar=99; lam->{ p myvar }; [1,2,3].map{|myvar| lam }

In other words: (Question 2:)

How can I define a macro (instead a lambda), so that this code is
executed as it would be written in that position. (If the same code is
used more times).

···

Am 23.08.21 um 18:42 schrieb Die Optimisten:

Am 23.08.21 um 18:35 schrieb Die Optimisten:

Am 23.08.21 um 18:31 schrieb Die Optimisten:

thank you

Opt

As far as I know, there are no macros in Ruby, but you can use meta-programming with eval; maybe this can solve your problem:

def create_method(num)
  str =
    "def plus_#{num}(num2); "\
    " return #{num} + num2; "\
    "end"

  eval(str)
end

create_method 2
puts plus_2(3)

···

________________________________
De: ruby-talk <ruby-talk-bounces@ruby-lang.org> em nome de Die Optimisten <inform@die-optimisten.net>
Enviado: segunda-feira, 23 de agosto de 2021 13:56
Para: ruby-talk@ruby-lang.org <ruby-talk@ruby-lang.org>
Assunto: Re: param-vars - example

Am 23.08.21 um 18:42 schrieb Die Optimisten:

Am 23.08.21 um 18:35 schrieb Die Optimisten:

Am 23.08.21 um 18:31 schrieb Die Optimisten:

Hi

What is the name of |x| - Vars ( a.map{|x| ... )

thanks

Opti

AND is it (somehow!) possible to use them in a pre-defined lambda
(like a local-var, without params for the lamba)?

Example: myvar=99; lam->{ p myvar }; [1,2,3].map{|myvar| lam }

In other words: (Question 2:)

How can I define a macro (instead a lambda), so that this code is
executed as it would be written in that position. (If the same code is
used more times).

thank you

Opt

Hi!

I agree that there are no macros in native Ruby, as far as i know.

To solve your problem, Passing the context into lambda (and receiving it in lambda) is necessary.

However, I have some idea on how to hide that fact.

I will combine `eval`, method alias and numbered parameters(since 2.7) to achieve that goal.


Kernel.alias_method :da, :binding
Proc.alias_method :-, :[]

var = 99

lam = ->{ eval('
  p var
', _1) }

[1, 2, 3].map{ |var| lam-da }

It should be better if I can reduce the presence of `var` but I have no idea on it.

Yunzhe

-----Original Messages-----

···

From:"Frederico de Oliveira Linhares" <fredlinhares@hotmail.com.br>
Sent Time:2021-08-24 02:25:41 (Tuesday)
To: "Ruby users" <ruby-talk@ruby-lang.org>
Cc:
Subject: RE: param-vars - example

As far as I know, there are no macros in Ruby, but you can use meta-programming with eval; maybe this can solve your problem:

def create_method(num)
  str =
    "def plus_#{num}(num2); "\
    " return #{num} + num2; "\
    "end"

  eval(str)
end

create_method 2
puts plus_2(3)

De: ruby-talk <ruby-talk-bounces@ruby-lang.org> em nome de Die Optimisten <inform@die-optimisten.net>
Enviado: segunda-feira, 23 de agosto de 2021 13:56
Para: ruby-talk@ruby-lang.org <ruby-talk@ruby-lang.org>
Assunto: Re: param-vars - example

Am 23.08.21 um 18:42 schrieb Die Optimisten:

Am 23.08.21 um 18:35 schrieb Die Optimisten:

Am 23.08.21 um 18:31 schrieb Die Optimisten:

Hi

What is the name of |x| - Vars ( a.map{|x| ... )

thanks

Opti

AND is it (somehow!) possible to use them in a pre-defined lambda
(like a local-var, without params for the lamba)?

Example: myvar=99; lam->{ p myvar }; [1,2,3].map{|myvar| lam }

In other words: (Question 2:)

How can I define a macro (instead a lambda), so that this code is
executed as it would be written in that position. (If the same code is
used more times).

thank you

Opt

This is the closest Ruby gets I think:

@myvar = 99
lam = ->{ p @myvar }
[1,2,3].map{|i|
  @myvar = i
  lam.call
}

If you don't want to pollute the main name space, then you can put that
in a module or class:

module MyVars
  @myvar = 99
  lam = ->{ p @myvar }
  [1,2,3].map{|i|
    @myvar = i
   
lam.call
  }
end

Warning: This is not thread-safe

It's kind of hard to parse the question. If you're asking about just not
naming the parameters, you can use:

···

~~~
lam = ->{ p _1 }
lam['hello'] #=> "hello"
~~~

But if you're talking about binding the lambda's scope to the call site I
think the actual answer is a combination of "no" and "why?"

Without a very deep and strong understanding of how and why Ruby lambdas
bind their scope (c.f. "closure") and about rebinding, nor acknowledging
the many parameterisation techniques Ruby provides and understanding why it
might lean so heavily into the functional programming model of
parameters-not-scope, and without a clear explanation of how and why those
parameterisation techniques don't work for your situation; I would have to
ask why you would want to fight against the language like that?

Remember that macros provide type abstraction in lower-level languages like
C -- not required in Ruby because of duck typing, and give programmers
tighter control over optimisation when you _really_ need that code to be
inlined and not a function call (and you know better than the compiler
(which you don't)) which is not relevant in Ruby -- you're much better
optimising algorithms, not function calls.

Cheers

On Tue, 24 Aug 2021, 02:44 Die Optimisten, <inform@die-optimisten.net> wrote:

Am 23.08.21 um 18:35 schrieb Die Optimisten:
> Am 23.08.21 um 18:31 schrieb Die Optimisten:
>> Hi
>>
>> What is the name of |x| - Vars ( a.map{|x| ... )
>>
>> thanks
>>
>> Opti
>
> AND is it (somehow!) possible to use them in a pre-defined lambda
> (like a local-var, without params for the lamba)?
>
Example: myvar=99; lam->{ p myvar }; [1,2,3].map{|myvar| lam }

> thank you
>
> Opt

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Hello!
- Thank you for your answers! -

My case is that if adding params, I don't want to change that in every
call of the method;
also for debugging purposes: just wanting to print out some values in
different places - because the vars [varnames] I want to get are
different each time (but the same in each place), I can't insert/change
them everywhere. Tharefore a macro. How can I achive that in a "nice" way?

Opti

···

Am 25.08.21 um 01:31 schrieb Matthew Kerwin:

It's kind of hard to parse the question. If you're asking about just
not naming the parameters, you can use:

~~~
lam = ->{ p _1 }
lam['hello'] #=> "hello" # IF USING Ruby >=2.7...
~~~

But if you're talking about binding the lambda's scope to the call
site I think the actual answer is a combination of "no" and "why?"

Without a very deep and strong understanding of how and why Ruby
lambdas bind their scope (c.f. "closure") and about rebinding, nor
acknowledging the many parameterisation techniques Ruby provides and
understanding why it might lean so heavily into the functional
programming model of parameters-not-scope, and without a clear
explanation of how and why those parameterisation techniques don't
work for your situation; I would have to ask why you would want to
fight against the language like that?

Remember that macros provide type abstraction in lower-level languages
like C -- not required in Ruby because of duck typing, and give
programmers tighter control over optimisation when you _really_ need
that code to be inlined and not a function call (and you know better
than the compiler (which you don't)) which is not relevant in Ruby --
you're much better optimising algorithms, not function calls.

Cheers

On Tue, 24 Aug 2021, 02:44 Die Optimisten, <inform@die-optimisten.net > <mailto:inform@die-optimisten.net>> wrote:

    Am 23.08.21 um 18:35 schrieb Die Optimisten:
    > Am 23.08.21 um 18:31 schrieb Die Optimisten:
    >> Hi
    >>
    >> What is the name of |x| - Vars ( a.map{|x| ... )
    >>
    >> thanks
    >>
    >> Opti
    >
    > AND is it (somehow!) possible to use them in a pre-defined lambda
    > (like a local-var, without params for the lamba)?
    >
    Example: myvar=99; lam->{ p myvar }; [1,2,3].map{|myvar| lam }

    > thank you
    >
    > Opt

    Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org
    <mailto:ruby-talk-request@ruby-lang.org>?subject=unsubscribe>
    <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Add them with a default value to avoid changing all call sites:

9979 % ruby -e 'proc { |x, y=nil| p [x, y] }.call 42'
[42, nil]

···

On Aug 25, 2021, at 01:36, Die Optimisten <inform@die-optimisten.net> wrote:

My case is that if adding params, I don't want to change that in every call of the method;