Similar Threads:
1.A block is an inside-out method [Was: info on block arguments]
> No. sort_by is taking the block and for each item in the array, it puts
> that item into |toy|, and then grabs the :shape from it.
A block is an everted (inside-out) method.
A normal function:
def foo(x)
x + 42
end
A similar block:
y = 42
block_user do |x|
x + y
end
The call site for foo would just look like foo(12). The name of foo is on
the outside, its calling parens are on the outside, and its behavior is
on the inside.
The block turns this pattern inside out. The call site (yield(12) or
block.call(12)) is inside the block_user.
The behavior is on the outside.
So when foo takes its argument, it needs delimiters to name the argument
for the interpreter. (x).
When block_user passes x, the block needs delimiters to name its
argument, so its block body can use it. |x|.
And because the behavior is on the outside, it can share variables from
its enclosing scope. foo(), by contrast, can only share variables from
its enclosing class.
--
Phlip
2.Default argument values for blocks
Is there a reason why I can't do this?
foo = lambda { |foo = bar| puts foo }
foo.call
I can't think of any good reason why this isn't valid.
Cheers,
Daniel Schierbeck
3.named method arguments with defaults
Hello all,
What is the current idiomatic approach to pass named arguments to a
method in Ruby ?
In Perl, I can do:
sub method
{
# first the defaults are specified, then @_ is appended and overrides
# the defaults, using Perl's array-folding-into-hash feature
#
my %args = ("size" => 45,
"length" => 12,
@_);
my $the_size = $args{"size"};
}
# now, a call: 44 will override the default size 45, but length will
remain 12
#
method("size" => 44);
I'm sure Ruby has a nearly similar approach to achieve this, I just not
skilled enough (yet) to know how. Please advise.
Thanks in advance
Eli
4.Defining a method with default arguments in C
Brian Takita wrote:
> Hello,
>
> How can I define a default argument value such as the following in c?
>
> def foobar(arg=1)
> end
>
> Thanks,
> Brian Takita
>
Specify the number of arguments as -1 in rb_define_method:
rb_define_method(myclass, "method_name", meth_fnc, -1);
In your method use rb_scan_args to scan the argument list. If you don't
get enough arguments, use the default values instead.
5.Defining a method with an argument with a default value
I have this line in a class method to define an initializer:
define_method(:initialize){|value| @value = value}
But I want something more like this:
define_method(:initialize){|value=nil| @value = value if value}
which doesn't seem to be possible because of the "value=nil" part. Is
there some way to do this that isn't eval? Eval would work fine since
this is pretty static code and I have unit tests for it but I try to
avoid it if I can. I've only used it before for performance to turn
some code with runtime tests into eval-time tests.
Pedro.
6. Default values for method arguments
7. default passed block for method
8. Methods with [] braces can't take block arguments?