[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

variable in lambda

salamond

6/5/2008 9:10:00 AM

Hi, all.

I came across the following problem.
I need to generate a couple of methods,
by now, I'm using
acode = lambda {
puts "i am a student in class " + c
}

bcode = lambda {
puts "class "+c+ "is a great class"
}

["a", "b", "c"].each do |c|
define_method("student_in_class"+c, acode)
define_method("greate_class"+c, bcode)
end

now ruby doesn't seem to recognize c in acode and bcode.
is there any alternatives?

thx
JarodZZ

9 Answers

Onur Gungor

6/5/2008 9:40:00 AM

0

salamond wrote:
> Hi, all.
>
> I came across the following problem.
> I need to generate a couple of methods,
> by now, I'm using
> acode = lambda {
> puts "i am a student in class " + c
> }

acode = lambda { |c| lambda {
puts "i am a student in class " + c
}

>
> bcode = lambda {
> puts "class "+c+ "is a great class"
> }

bcode = lambda { |c| lambda {
puts "class "+c+ "is a great class"
}
}

>
> ["a", "b", "c"].each do |c|
> define_method("student_in_class"+c, acode)
> define_method("greate_class"+c, bcode)
> end
["a", "b", "c"].each do |c|
define_method("student_in_class"+c, acode.call(c))
define_method("greate_class"+c, bcode.call(c))
end
>
> now ruby doesn't seem to recognize c in acode and bcode.
> is there any alternatives?
>
> thx
> JarodZZ

I think this will do the trick.

onur
--
Posted via http://www.ruby-....

Onur Gungor

6/5/2008 9:44:00 AM

0


a missing curly brackets.

>
> acode = lambda { |c| lambda {
> puts "i am a student in class " + c
> }}
>
--
Posted via http://www.ruby-....

Robert Klemme

6/5/2008 5:42:00 PM

0

On 05.06.2008 11:43, Onur Gungor wrote:
> a missing curly brackets.
>
>> acode = lambda { |c| lambda {
>> puts "i am a student in class " + c
>> }}
>>

Much simpler

irb(main):001:0> class F
irb(main):002:1> %w{a b c}.each {|c| define_method(c) { c }}
irb(main):003:1> end
=> ["a", "b", "c"]
irb(main):004:0> F.new.a
=> "a"
irb(main):005:0> F.new.b
=> "b"
irb(main):006:0> F.new.c
=> "c"
irb(main):007:0>

Kind regards

robert

salamond

6/6/2008 1:41:00 AM

0

many thanks for the help, Robert and Onur.

I tried Onur's way, it works.
But I don't quite get the later simpler method, I'll try it later.

I'm using this method in UnitTest.
Have you guys wondered, why Ruby Unit Test can't do something like this:

class Test_${name} {
def setup {}
def test_${step1} {}
def test_${step2} {}
def teardown {}
}

most of my unit test cases are like something above, and I want to
express all cases in data,
automatically generate cases in the run time

Do u think it's possible?

JarodZZ~

On Fri, Jun 6, 2008 at 1:44 AM, Robert Klemme
<shortcutter@googlemail.com> wrote:
> On 05.06.2008 11:43, Onur Gungor wrote:
>>
>> a missing curly brackets.
>>
>>> acode = lambda { |c| lambda {
>>> puts "i am a student in class " + c
>>> }}
>>>
>
> Much simpler
>
> irb(main):001:0> class F
> irb(main):002:1> %w{a b c}.each {|c| define_method(c) { c }}
> irb(main):003:1> end
> => ["a", "b", "c"]
> irb(main):004:0> F.new.a
> => "a"
> irb(main):005:0> F.new.b
> => "b"
> irb(main):006:0> F.new.c
> => "c"
> irb(main):007:0>
>
> Kind regards
>
> robert
>
>

Robert Klemme

6/6/2008 6:31:00 AM

0

On 06.06.2008 03:40, salamond wrote:
> I'm using this method in UnitTest.
> Have you guys wondered, why Ruby Unit Test can't do something like this:
>
> class Test_${name} {
> def setup {}
> def test_${step1} {}
> def test_${step2} {}
> def teardown {}
> }
>
> most of my unit test cases are like something above, and I want to
> express all cases in data,
> automatically generate cases in the run time
>
> Do u think it's possible?

A lot is possible. But from what you write it is not clear what "it" in
this case is. Can you be more concrete?

Kind regards

robert

salamond

6/7/2008 7:23:00 PM

0

sure, check below,
see I have cases like:

TC1 = { name=>"touch", setup => "mkdir1 ", test_step1=>"touch file",
test_step2=>"cat file", teardown => "rm dir 1"}
TC2 = { name=>"echo" setup => "mkdir 2", test_step1=>"echo 1 > file",
test_step2=>"cat file", teardown => "rm dir 2"}

Can I use a case_reader or case_generator to generate 2 testcases for
the above data?

thx
JarodZZ

On Fri, Jun 6, 2008 at 2:33 PM, Robert Klemme
<shortcutter@googlemail.com> wrote:
> On 06.06.2008 03:40, salamond wrote:
>>
>> I'm using this method in UnitTest.
>> Have you guys wondered, why Ruby Unit Test can't do something like this:
>>
>> class Test_${name} {
>> def setup {}
>> def test_${step1} {}
>> def test_${step2} {}
>> def teardown {}
>> }
>>
>> most of my unit test cases are like something above, and I want to
>> express all cases in data,
>> automatically generate cases in the run time
>>
>> Do u think it's possible?
>
> A lot is possible. But from what you write it is not clear what "it" in
> this case is. Can you be more concrete?
>
> Kind regards
>
> robert
>
>

matt.orel

8/12/2008 3:49:00 AM

0

On Aug 11, 9:37 pm, "Rick Rubenstein" <pound...@optonline.net> wrote:
> "SMBalloon" <smball...@aol.com> wrote in message
>
> news:57p0a4tpugl4kq389105ju8nr3fqeh5f0p@4ax.com...
>
>
>
> > On Mon, 11 Aug 2008 12:19:00 GMT, "Robert Wiersema"
> > <robwISNTATh...@THISshaw.ADDRESSca> wrote:
>
> >>"SMBalloon" <smball...@aol.com> wrote in message
> >>news:kk80a4hafjvrl1ie92m7v8ujs1pmoamjp6@4ax.com...
> >>> On Mon, 11 Aug 2008 06:11:52 -0400, "Rick Rubenstein"
> >>> <pound...@optonline.net> wrote:
>
> >>>>Google gives money to Democrats. . .. therefore it must be implying that
> >>>>their employees should vote Democrat."
>
> >>> It was Google employees which gave 98% to Democrats.  That's about as
> >>> high a percentage of the votes that leaders in the old Soviet union
> >>> got.  There are really only 2 ways that 98% of *employee*
> >>> contributions could go to one party.  Either you deliberately seek to
> >>> not hire Republican supporters (imagine the outcry if Walmart policy
> >>> was to avoid hiring Democratic supporters) -- or there is a massive
> >>> implication from company heads that employees should support
> >>> Democrats, or not support Republicans.  Again, I don't see how you get
> >>> to 98% any other way  And I really doubt you do either.
>
> Wow. More logical fallacy at work.
> I know this is hard for you to accept, but the Google workforce is
> visionary, forward-thinking, highly educated, and highly informed.
> That would pre-dispose them toward supporting a candidate that knows how to
> use a computer, for instance, as opposed to one who admitted that
> he doesn't even know how to surft the internet (as McCain admitted) or
> perhaps one who finished fifth from the bottom of his class at Annapolis,
> after having
> gotten in as a legacy.
>
> Now, there IS another way to look at it, of course: Google employees might
> send contributions to the candidate they expect to be helpful to their
> industry, since they are shareholders, by and large---and you can make a
> pretty good case that the youth of the tech industry also lends itself to
> overwhelming Democratic support. Read the article at:http://seattlepi.nwsource.com/business/358889_softw...
> for statistics and background showing that pretty much ALL major techie
> companies overwhelmingly supported Obama. This analysis, however fails the
> Balloon logical fallacy test.
> For Google workers, looming internet taxes are a big issue, and  offshoring
> of jobs. Democrats tend to be more protectionist, and more up-front in the
> battle against internet taxes, which could account for some of the support.
>
> But really, Balloon, if you're looking for the reason why Google
> shareholders/employees aren't big fans of the GOP, look no further than
> Denny Hastert and Joe Barton, who threw major monkey wrenches into the
> acquisition of Double-click, investigating Google and depressing the stock
> price for awhile, as well as costing months and $$$$ during the
> Congressional investigations. Take a look at Google's position on
> net-neutrality, a HUGE issue for the shareholders and employees, and a
> position fought against by Google adversaries like Verizon, AT&T and
> Comcast, among others--companies that throw tons of cash at the GOP.
>
> The bottom line for me on this whole tired non-argument is that the Labor
> Bill doesn't say what Wal-Mart is telling its employees it says, and that's
> plain as day. Any casual reader should be able to cull out that fact, and
> hate their tactic for what it is. Analogies to Google fail for a lot of
> reasons, but the best reason is that there is NO EVIDENCE WHATSOEVER that
> Google has lied to its employees or used pressure tactics combined with
> outright falsehoods on a major piece of legislation in order to chill
> political support for a specific candidate.

Or, even more direct: There is no evidence from the data presented
that Google has taken any position at all to its employees. I thought
the whole "correlation = causation" argument generally gets disposed
of in the first day of logic study; not, apparently, for Balloon.

matt.orel

8/13/2008 12:08:00 AM

0

On Aug 12, 6:41 pm, Ukes <duke_of_did...@hotmail.com> wrote:
> On Mon, 11 Aug 2008 20:49:21 -0700 (PDT), matt.o...@gmail.com wrote:
> >  I thought
> >the whole "correlation = causation" argument generally gets disposed
> >of in the first day of logic study;
>
> Although it's true that correlation doesn't *prove* causation, the two
> are not unrelated since there can't be causation without correlation.
> Correlation is necessary to prove causation, but not sufficient to
> prove it.

Correct on the 2nd statement. wrt the first, an example: There is a
correlation between where lemon trees grow and where orange trees
grow. But, the growth of lemon trees does not cause the growth of
orange trees. So far as I'm concerned, they're unrelated.

matt.orel

8/13/2008 12:22:00 AM

0

On Aug 12, 9:15 am, SMBalloon <smball...@aol.com> wrote:
> On Mon, 11 Aug 2008 20:49:21 -0700 (PDT), matt.o...@gmail.com wrote:
> >Or, even more direct:  There is no evidence from the data presented
> >that Google has taken any position at all to its employees.  I thought
> >the whole "correlation = causation" argument generally gets disposed
> >of in the first day of logic study; not, apparently, for Balloon.
>
> We're talking 98%, not 80% or 75% of a decent sized domain.  The only
> way I think that is mathematically plausible is if Google either
> deliberately tries to avoid hiring Republican supporters, of if there
> is an implied or explicit company desire for its employees to either
> support Democrats or not support Republicans.

Is that what passes for logic in balloon-land? Or is the above
balloon-speak for "Balloon's got nuthin'"?

First, let's do a sanity check: The figures were of known donations
for the *2004* election cycle. Google had been public all of 3 months
at the time of the election, it was a relatively small company, and
its total donation amount barely cleared 200k. The article doesn't
even state how many donors were involved; we're probably looking at
less than a thousand -- and they're probably among the first hires.
So, no, it's not a particularly decent sized domain; it's tiny.

Next, you show a profound lack of understanding of Google. Really,
rather comical on several levels.

Then, you compare this to documentary evidence of an employer trying
to coerce its employees in to voting a certain way! That's precious.
Where's your corresponding evidence for Google, or anyone else?

The rest of your post is, frankly, too logically bizarre for much in
the way of serious response.
I will note two things: 1) If you find that 98% of Google's 2008
money is going to Cynthia McKinney, I'll give you a mulligan on a
"correlation=causation" charge. 2) We're not talking coin flips here.

>
> Let's reverse the scenario.  Let's say that 98% of Walmart employee
> contributions went to Republicans and only 2% to Democrats.  Would you
> not consider that mathematically conclusive proof that Walmart either
> tired to avoid hiring Democrats or that Walmart was either implicitly
> or explicitly making it clear to their employees that they should
> support Republicans and not Democrats?  
>
> As well, you relied on similar type mathematical reasoning to know
> beyond any doubt that thousands of people in Jewish parts of Palm
> Beach County really didn't intend to vote for Pat Buchanan back in
> 2000.  You had no problem engaging in the "correlation = causation"
> argument back then.  And that's because the numbers were so
> overwhelmingly supportive of that.  If someone accused you of engaging
> in "logical fallacy" regarding that, you would have laughed.    
>
> I guess it's mathematically possible to flip a coin 25 times and have
> it come out heads 24 times.  But it's not mathematically plausible
> that this would happen unless something else was at play.  And when
> you get 98% of the contributions of employees of a sizable company
> going to Democrats, then it's far more reasonable to think there is
> something at play besides Google just having highly educated and
> highly informed employees who of course would never vote for a
> Republican.