[Chunky_bacon] instance variables not inherited?

Robert Fletcher lobatifricha at gmail.com
Wed Jul 6 20:36:49 UTC 2011


Yes, classes inherit all instance variables from their parent classes.  But
if the instance variable has not been set then it will register as
undefined.  Instance variables are private by default, so you either need to
write accessor methods or let Ruby do it for you by using attr_accessor,
attr_reader, or attr_writer:

class A
  attr_accessor :a

  def initialize
    @a = "a!"
  end
end

class B < A
end

b = B.new
puts b.a

As for rescue blocks, ensure happens no matter what.  See the following:

begin
  puts nil.blah
rescue Exception => ex
  puts "Wah!"
  raise "wah"
else
  puts "else"
ensure
  puts "ensure"
end

Else happens only if there is no error.  Not actually all that useful.

On Wed, Jul 6, 2011 at 1:15 PM, <travis+ml-chunky-bacon at subspacefield.org>wrote:

> Ooops that was my mistake.  I was failing to set the variable.
>
> That reminds me:
>
>        begin
>          ...
>        # NOTE: do not just trap RuntimeException here, since we want to
> catch SIGINT (^c)
>        rescue
>          # TODO: Is there a better way to do this rescue/ensure stuff?
>          bv.umount()
>          # propogate exception
>          raise
>        else
>          # TODO: as noted above, this is tedious.
>          bv.umount()
>        end
>
> I used to have the code expanded out, but now I've encapsulated it
> into bv.umount, so it's not nearly as bad, though there is a violation
> of DRY, I think.
>
> IIRC, I couldn't use ensure since it won't propogate the exception, or
> if I raised during the rescue, the ensure clause wouldn't get run - I
> believe because it's not in the begin block, and so raising from within
> rescue causes it to avoid running ensure.  I could be misremembereing,
> though, it has been a while.
> --
> http://www.subspacefield.org/~travis/ | Meijin ni joseki nashi
> "Who wants flowers when you're dead?  Nobody." -- Catcher in the Rye
> If you are a spammer, please email john at subspacefield.org to get
> blacklisted.
>
> _______________________________________________
> Chunky_bacon mailing list
> Chunky_bacon at lists.noisebridge.net
> https://www.noisebridge.net/mailman/listinfo/chunky_bacon
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.noisebridge.net/pipermail/chunky_bacon/attachments/20110706/37a0f568/attachment-0003.html>


More information about the Chunky_bacon mailing list