TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ask HN: What does this ruby snippet print and why?

1 pointsby jhuckesteinover 11 years ago
<p><pre><code> class Base def something puts &quot;called something base&quot; return {a: 1, b: 2} end end class Extended &lt; Base def some_method puts &quot;something is {a: 1, b: 2}:&quot; puts something.inspect if false puts &quot;doesn&#x27;t get executed&quot; something = {} else puts &quot;something is now nil:&quot; puts something.inspect end end end e = Extended.new e.some_method </code></pre> Edit: formatting

1 comment

jhuckesteinover 11 years ago
<p><pre><code> something is {a: 1, b: 2}: called something base {:a=&gt;1, :b=&gt;2} something is now nil: nil </code></pre> In the end something is nil (instead of {a:1, b:2}) for me on ruby 1.9.3p194 and 2.0.0-p247. Does anyone know why this happens?