From: timr Date: 2010-11-12T13:25:20+09:00 Subject: Re: Scheme's (cond ((assertion) (value))...(else (value))) statement implemented in ruby? On Nov 11, 5:51 pm, Clifford Heath wrote: > timr wrote: > > Hi Rubyists, > > I love ruby, but am learning scheme and kinda like the (cond > > ((assertion) (value))...(else (value))) syntax for what would normally > > look like this in ruby: > > > if (assertion 1) > >   value 1 > > elsif (assertion 2) > >   value 2 > > else > >   default > > end > > > I find scheme's cond syntax more quickly digestible since it reads > > like a table. > > (cond > >     ((assertion 1) (value 1)) > >     ((assertion 2) (value 2)) > >                 (else (default))) > > What's wrong with this? > > x = 3 > case > when x == 2; puts 'two' > when x == 3; puts 'three' > else puts 'other' > end > > Clifford Heath. Case interrogates a single variable for its state--whether it falls in a certain range, or is equal to such and such a value. The cond statement is much more flexible, in that each assertion can be completely unrelated to the other assertions--i.e. they can cover more than the state of a single variable. cond l{Store.open_time..Store.close_time === Time.now} =>l{Store.access = true}, l{after_hours_users.include? User.name} =>l{Store.access = true}, cond_end =>l{Store.access = false; User.rain_check(1)} In the above code access is given to anyone during store hours, but if it is not during store hours and the user is listed as an after_hours_user, they are allowed acces. If they can't get in because it is after hours and they are not listed as an after_hours_user, they are denied access but given a rain_check. You can't do this kind of complex functionality with case, which only inquires with regards to one variable. Tim