|
|
|
|
|
by inopinatus
2093 days ago
|
|
Ruby already had a sort of limited form of rightward assignment, in exception rescue: begin
#... do something
rescue StandardError => ex
#... handle exception
end
The ex here can be any assignment expression, it's not just a lexical variable. So you could already do this: class Guru
def meditate=(exception)
puts "caught #{exception.inspect}"
end
end
# later ...
rescue OutOfCheeseError => Guru.new.meditate
i.e. creating a nice opportunity for passing error handlers around as an argument; or, this worrying idea: rescue TransactionError => Thread.current[:exception]
and now I'm afraid you can do this: Cat = Struct.new(:name) do
class << self
def all() = @all ||= Set.new
def adopt=(...)
all << new(...)
end
end
end
"Astrid" => Cat.adopt
|
|