begin/end doesn't behave as expected 
Author Message
 begin/end doesn't behave as expected

Why doesn't this snippet of code do what I expect it to?

out = "<a href=\""
out << begin
        "error.rb"
        "rules.rb" if rule.instance_of? Rule
        "dead.rb" if rule.instance_of? DeadRule
        "proposals.rb" if rule.instance_of? Proposal
        page.to_s unless page.nil?
end
out << "\">" + rule.to_s + "</A>"

I expect the begin/end block to evaluate to one of page.to_s,
"proposals.rb", "dead.rb", "rules.rb" or if all else fails "error.rb" but
instead it seems to evaluate to nil, and I get "TypeError: failed to
convert nil into String".

Tim Bates
--



Sat, 07 May 2005 18:25:04 GMT  
 begin/end doesn't behave as expected

Quote:

>Why doesn't this snippet of code do what I expect it to?

>out = "<a href=\""
>out << begin
>        "error.rb"
>        "rules.rb" if rule.instance_of? Rule
>        "dead.rb" if rule.instance_of? DeadRule
>        "proposals.rb" if rule.instance_of? Proposal
>        page.to_s unless page.nil?
>end
>out << "\">" + rule.to_s + "</A>"

>I expect the begin/end block to evaluate to one of page.to_s,
>"proposals.rb", "dead.rb", "rules.rb" or if all else fails "error.rb" but
>instead it seems to evaluate to nil, and I get "TypeError: failed to
>convert nil into String".

>Tim Bates

I think a case statement will do what you want here..


Sat, 07 May 2005 21:13:42 GMT  
 begin/end doesn't behave as expected

 For the original message, which is not in [ruby-talk]


Quote:
>>Why doesn't this snippet of code do what I expect it to?

>>out = "<a href=\""
>>out << begin
>>        "error.rb"
>>        "rules.rb" if rule.instance_of? Rule
>>        "dead.rb" if rule.instance_of? DeadRule
>>        "proposals.rb" if rule.instance_of? Proposal
>>        page.to_s unless page.nil?
>>end

 The block return the value of the last expression which in this case is

    page.to_s unless page.nil?

  i.e.

    unless page.nil?
       page.to_s
    else
       nil
    end

 because probably (???) page is nil, it return the value of the `else' part

T> I think a case statement will do what you want here..

 yes

Guy Decoux



Sat, 07 May 2005 22:03:31 GMT  
 begin/end doesn't behave as expected

Quote:
>  For the original message, which is not in [ruby-talk]

Huh? I just subscribed to the newsgroup, is there a mailing list as well?

Quote:
>  because probably (???) page is nil, it return the value of the `else'
>  part

Yes, page is nil. This is the response I was looking for, thankyou.

Quote:
> T> I think a case statement will do what you want here..

How would I use a case statement here? I'm comparing two different things,
namely rule.instance_of? and page.nil?
Also, rule.type == Rule behaves as expected but rule.type === Rule always
seems to return false, so it's no use comparing rule.type in case
expression; what should I do?

(The original code snippet again...)

out = "<a href=\""
out << begin
        "error.rb"
        "rules.rb" if rule.instance_of? Rule
        "dead.rb" if rule.instance_of? DeadRule
        "proposals.rb" if rule.instance_of? Proposal
        page.to_s unless page.nil?
end
out << "\">" + rule.to_s + "</A>"

If page is non-nil, this should return page.to_s; otherwise, it returns a
string based on whether rule is of class Rule or one of its two children.
If all else fails, ie rule is not a Rule (which it should be), then return
"error.rb".

Tim Bates
--



Sun, 08 May 2005 05:56:17 GMT  
 begin/end doesn't behave as expected
[...]

Quote:
> > T> I think a case statement will do what you want here..

>  How would I use a case statement here? I'm comparing two different things,
>  namely rule.instance_of? and page.nil?
[...]
>  out << begin
>          "error.rb"
>          "rules.rb" if rule.instance_of? Rule
>          "dead.rb" if rule.instance_of? DeadRule
>          "proposals.rb" if rule.instance_of? Proposal
>          page.to_s unless page.nil?
>  end

out << if page then
  page.to_s
else
  case rule                       # Note: not rule.type
  when Proposal then "proposals.rb"
  when DeadRule then "dead.rb"
  when Rule then "rules.rb"
  else "error.rb"
  end
end

Untested, but you get the idea.

[...]

                        Reimer Behrends



Sun, 08 May 2005 07:13:51 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. re.LOCALE | re.IGNORECASE doesn't work as you'd expect

2. expect's full_buffer doesn't seem to work

3. Why doesn't this work as expected?

4. tkdesk on fedora core 5 -- doesn't run, doesn't compile

5. Expect doesn't know when to quit

6. tcl/expect doesn't work unless I telnet into the machine

7. expect doesn't like...the failed attachment

8. EXPECT - Doesn't wait for Prompt!

9. expect script doesn't work with cron - please help

10. Expect script doesn't work !!

11. BLT installs but doesn't work, and TkTable doesn't build

12. Expect install: directory doesn't contain private headers

 

 
Powered by phpBB® Forum Software