zw963
February 20, 2023, 10:14am
1
ary = {"foo", "bar"}
case "bar"
when *ary
puts "1"
else
puts "2"
end
In 1.cr:4:6
4 | when *ary
^
Error: unexpected token: "*"
I just like to do things like this in Ruby.
ary = ["foo", "bar"]
case "bar"
when *ary
puts "1"
else
puts "2"
end
# => 1
Thanks.
This is the Crystal way:
if ary.includes?("bar")
puts "1"
else
puts "2"
end
I guess it would make sense to have it as a language feature. That said, I never used it. I don’t think it’s that common or useful. So maybe not… it might even be confusing.
zw963
February 20, 2023, 1:11pm
4
surprise, I used it regularly, my several gem and project both use this splating.
ary = {"foo", "bar"}
case "bar"
when *ary
puts "1"
else
puts "2"
end
same as
case "bar"
when *{"foo", "bar"}
puts "1"
Same as
case "bar"
when "foo", "bar"
puts "1"
Why you think this confusing?
zw963
February 20, 2023, 3:26pm
5
I get answer from Blacksmoke16 in this closed issue
Following is the accepted answer:
ary = {"foo", "bar"}
case "bar"
when .in? ary
puts "1"
else
puts "2"
end
1 Like