[#403837] Why none of the block giving the expected output with the "enumerator"? — Arup Rakshit <lists@...>

Why none of the block giving the expected output with the "enumerator"?

9 messages 2013/02/02

[#403870] Confusion with Enum#with_object block argument construct — Arup Rakshit <lists@...>

C:\>irb

9 messages 2013/02/03

[#403920] Character classes use in Ruby — Love U Ruby <lists@...>

Can anyone help me by giving an explanatory example of each of the

13 messages 2013/02/04

[#403935] How to stop page loading using selenium-web driver? — Love U Ruby <lists@...>

How to stop page loading using selenium-web driver?

11 messages 2013/02/04

[#403972] Ruby could recognize the values when putting into a webpage text filed. — Love U Ruby <lists@...>

Hi,

9 messages 2013/02/05

[#403986] old syntax? what's going on here — tamouse mailing lists <tamouse.lists@...>

I have this in a _spec.rb file: (a gem I inherited at work)

11 messages 2013/02/05

[#404005] Implementing DRY with a function call — Rob Marshall <lists@...>

Hi,

12 messages 2013/02/05

[#404006] using an instance variable inside a method — FirstName Surname <lists@...>

Hello.

19 messages 2013/02/05

[#404021] Not able get the label text incurred with <input> element — Love U Ruby <lists@...>

I do have a below `HTML`:

18 messages 2013/02/05

[#404025] Symbol.defined? — Student Jr <lists@...>

Symbol deserialization from external sources is now known to be

15 messages 2013/02/06

[#404058] Ruby 1.9.3-p362 on Mac OSX — Peter Bailey <lists@...>

Hi,

13 messages 2013/02/06

[#404082] Problem building Ruby 1.9.3 patchlevel 385 under AIX 7.1 — Ruby Student <ruby.student@...>

Hello World!

10 messages 2013/02/06

[#404101] Issues from an extreme beginner — Bruce Palmer <lists@...>

Hey guys, great to be part of such a great community! I look forward to

19 messages 2013/02/07
[#404104] Re: Issues from an extreme beginner — Bruce Palmer <lists@...> 2013/02/07

Ahh, thank you Matthew! That was just the push I needed!

[#404208] elegant way to determine if something is defined — tamouse mailing lists <tamouse.lists@...>

Something like:

15 messages 2013/02/10

[#404218] Ruby Equivalent to VB's "With"? — Joel Pearson <lists@...>

I've looked around but I couldn't find anything helpful on this,

11 messages 2013/02/10

[#404235] The "ruby way" to do desktop applications? — "guirec c." <lists@...>

Hello,

17 messages 2013/02/11

[#404238] Best books for "advanced" programmers — "guirec c." <lists@...>

Hello,

18 messages 2013/02/11

[#404245] Issue with Excel column values read. — Love U Ruby <lists@...>

Hi,

22 messages 2013/02/11

[#404344] Ruby command line options s and S — Love U Ruby <lists@...>

Can anyone help me to understand the difference between s and S with

15 messages 2013/02/13

[#404386] Re: Ruby command line options s and S — "D. Deryl Downey" <me@...>

Dude!

20 messages 2013/02/14
[#404397] Re: Ruby command line options s and S — Love U Ruby <lists@...> 2013/02/14

Humm!

[#404387] Ruby Multithreaded producer-consumer problem — Abhijit Sarkar <lists@...>

Hi,

26 messages 2013/02/14
[#404896] Re: Ruby Multithreaded producer-consumer problem — Abhijit Sarkar <lists@...> 2013/02/24

Bump!

[#404456] skip iteration in each loop — Saurav Chakraborty <lists@...>

I want to skip iteration for few values depending on dynamic condition.

11 messages 2013/02/15

[#404491] so, what's the proper way to replace funcionality of GOTO ? — "Stu P. D'naim" <lists@...>

I need to make few scripts for tasks I do often manually, but last time

27 messages 2013/02/15
[#404492] Re: [from BASIC to Ruby] so, what's the proper way to replace funcionality of GOTO ? — Love U Ruby <lists@...> 2013/02/15

Stu P. D'naim wrote in post #1097111:

[#404494] Re: [from BASIC to Ruby] so, what's the proper way to replace funcionality of GOTO ? — Ryan Victory <ryan@...> 2013/02/15

Love U Ruby: I'm really not sure what you meant by that response, but

[#404570] What is Ruby's default constructor? — Love U Ruby <lists@...>

Hi,

12 messages 2013/02/17

[#404632] Re: splat operator and Ruby instance variable assignments — Marc Heiler <lists@...>

Ok, understood what the guy wants ...

17 messages 2013/02/19
[#404640] Re: splat operator and Ruby instance variable assignments — Love U Ruby <lists@...> 2013/02/19

Marc Heiler wrote in post #1097736:

[#404645] Re: splat operator and Ruby instance variable assignments — Ryan Davis <ryand-ruby@...> 2013/02/19

[#404646] Re: splat operator and Ruby instance variable assignments — Love U Ruby <lists@...> 2013/02/19

Ryan Davis wrote in post #1097840:

[#404647] Re: splat operator and Ruby instance variable assignments — Peter Hickman <peterhickman386@...> 2013/02/19

On 19 February 2013 20:35, Love U Ruby <lists@ruby-forum.com> wrote:

[#404648] Re: splat operator and Ruby instance variable assignments — Love U Ruby <lists@...> 2013/02/19

Peter Hickman wrote in post #1097848:

[#404696] THE CLASS/OBJECT CHICKEN-AND-EGG PARADOX — "Xavier R." <lists@...>

Hi,

25 messages 2013/02/20
[#404699] Re: THE CLASS/OBJECT CHICKEN-AND-EGG PARADOX — Matt Mongeau <halogenandtoast@...> 2013/02/20

Maybe you could provide more detail about what you are confused about. To

[#404700] Re: THE CLASS/OBJECT CHICKEN-AND-EGG PARADOX — "Xavier R." <lists@...> 2013/02/20

Matt Mongeau wrote in post #1098058:

[#404705] Re: THE CLASS/OBJECT CHICKEN-AND-EGG PARADOX — Matt Mongeau <halogenandtoast@...> 2013/02/20

It's not really a paradox. Take for example

[#404738] backslash substitution — Mario Ruiz <lists@...>

don't know why... but this is not working

18 messages 2013/02/21

[#404809] Difference of 2 dates interms of years. — "Xavier R." <lists@...>

how can we get the experience years between two dates(ex:2012-01-11 to

13 messages 2013/02/22

[#404817] Not able to understand the difference between "||=" and "|=". — "Xavier R." <lists@...>

>> a = []

12 messages 2013/02/22

[#404839] range is not assigning to the splat variable. — Love U Ruby <lists@...>

Why splat variable couldn't take in the below two code the "range" ->

10 messages 2013/02/23

[#404842] Why class returning its own name when "include" statement? — Love U Ruby <lists@...>

I was actually playing around with the class definition return values.

16 messages 2013/02/23
[#404844] Re: Why class returning its own name when "include" statement? — Love U Ruby <lists@...> 2013/02/23

@Stefano Yes you are right. The below code is proved that.

[#404867] how to see the class creation time in Ruby? — Love U Ruby <lists@...>

In Ruby any chance to see the last-modified time of a specific class?

15 messages 2013/02/23

[#404901] Confusion with `nil` value being produced by IRB in case of Array#size manipulation. — Love U Ruby <lists@...>

enum[int] = obj → obj

10 messages 2013/02/24

[#404921] How should I print only the last combination when using Array#combination(n) ? — Love U Ruby <lists@...>

>> a = [1,2,3]

15 messages 2013/02/24

[#405026] Please, help (GCD) greatest common divisor. — Caddy Tonks Lupin <lists@...>

Write a program to read two integers and show their greatest common

17 messages 2013/02/26

[#405059] Does this specific sound library exist? — Dirk Vogel <lists@...>

Hi there,

16 messages 2013/02/26

[#405067] Mac OS 10.8.2 and openssl — "Dr. Hegewald" <hegewald@...>

Hi everybody,

24 messages 2013/02/27

[#405079] Why `10` not returned without the `return` from the block ? — Love U Ruby <lists@...>

CODE - I

10 messages 2013/02/27

[#405107] Object track llist for a particular class. — "Xavier R." <lists@...>

Say I have created more than one instances from a particular class as

13 messages 2013/02/27

[#405145] Discussion on Ruby's `alias` — Tukai Patra <lists@...>

>> class Foo

27 messages 2013/02/28

[#405175] telnet - how to loop through commands listed in a file — Bob Ford <lists@...>

Let me first explain what I'm trying to do. I have written a very

18 messages 2013/02/28

Re: Implementing DRY with a function call

From: Matthew Kerwin <matthew@...>
Date: 2013-02-07 03:20:02 UTC
List: ruby-talk #404098
On 7 February 2013 10:39, Rob Marshall <lists@ruby-forum.com> wrote:

> Every other language I've worked in, C, Perl, Python, Java...the way you
> call a function with arguments is: func(arg1, arg2, arg3). In Python you
> can have variable args and keyword args specified as (*args, **kwargs),
> but in general you define the arguments that a function expects, and
> then pass those arguments between parentheses.


Give or take a few other var-args tricks.  Remember C has stdarg.h and
allows function signatures like:

    void func(...);

And Java has special array syntactic sugar:

    public static void func(Object... args)

And PHP almost completely ignores the variables named in the function
signature, because you can always use func_num_args() and func_get_args()

etc.

Ruby allows non-parenthetical args, the same as shell script (e.g. Bash).
 Technically perl doesn't require parentheses either, except that functions
in perl expect a list of parameters, and perl lists use parentheses, etc.
etc.  Upshot: `$thingy->foo` is also a function call in perl.



> So the idea that I could
> do:
>
> func(arg1,arg2,arg3) { block }
>
> And a) Not get a syntax error, and b) Be able to define the parameters
> for the function as:
>
> def func(*args)
> yield
> end
>
> and not only get the args specified between the parens in *args, but
> also still pass the block and "yield" it, is a very new, and foreign,
> concept for me.
>

If it helps, you can also define it as:

    def func(*args, &a_block)

if you want to make explicit the fact that there is (or expects to be) a
block.  It also gives you direct access to the block, so you can poke it
and stuff.



> What I find a bit confusing about Ruby is that, given:
>
> def func(*args)
> args.each { |a| puts a }
> yield
> end
>
> Then do the following:
>
> >> func "this", "is", "a", "test", return_value("something")
> this
> is
> a
> test
> This is the returning value: something
> LocalJumpError: no block given
>         from (irb):108:in `func'
>         from (irb):110
>
> This one is sort-of obvious since the function return_value (which is
> just a dummy function that simply shows calling a function with some
> arbitrary value and returning it slightly modified...so don't get hung
> up on it) is evaluated prior to being passed as an argument to func. But
> since there's no block, I get the LocalJumpError.
>

To the first remark:  this is the same as any other programming language.
 For example in Java:

    func("this", "is", "a", "test", return_value("something"));

...will execute the 'return_value' function first, then pass its result as
the final parameter to 'func'.

As an aside to the second: You can get around/preempt the LocalJumpError by
wrapping `yield` thus:

    def func(*args)
      args.each { |a| puts a }
      yield if block_given?
    end

Not necessarily what you want to do here, but handy to know.


>> func "this", "is", "a", "test", { return_value("something") }
> SyntaxError: compile error
> (irb):111: odd number list for Hash
>         from (irb):111
>
> This is is also somewhat obvious because it isn't treating the {} as
> defining a block but attempting to define a hash...That's because I
> haven't "formally" defined the arguments, so the interpreter assumes
> everything is supposed to be an argument and not a block.
>

That would happen irrespective of how you define the function parameters.
SyntaxErrors happen when ruby is parsing and interpreting the file, not
when it's executing.  A curly brace that follows a comma is _always_
interpreted as the start of a Hash literal.

Since blocks are special, "out-of-band" parameters, you don't need a comma
to distinguish them from the other args.  However, since curly-brace block
syntax looks a lot like Hash literal syntax, you need to give the parser a
bit more help in this case (i.e. by explicitly wrapping the "real" args in
parens, then giving the block.)


> >> func("this", "is", "a", "test") { return_value("something") }
> this
> is
> a
> test
> => "This is the returning value: something"
>
> This is still the odd one for me that now (sort-of) makes sense but
> would NOT be allowed in any language I've used in the past. So it's
> going to take some getting used to...


This is true, but only because those other languages don't have blocks.
 Note that the following calls are equivalent:

    func("this", "is", "a", "test") { return_value("something") }

    func "this", "is", "a", "test" do
      return_value "something"
    end

    my_block = proc { return_value("something") }
    func "this", "is", "a", "test", &my_block

    my_block = proc { return_value("something") }
    func("this", "is", "a", "test", &my_block)

The third and fourth versions are the call-site equivalent of the `def
func(*args, &a_block)` definition I mentioned above.

I hope this helps shed a little light on the issue, and gives you some
pointers for further research.

-- 
  Matthew Kerwin, B.Sc (CompSci) (Hons)
  http://matthew.kerwin.net.au/
  ABN: 59-013-727-651

  "You'll never find a programming language that frees
  you from the burden of clarifying your ideas." - xkcd

In This Thread