From: Nolic Date: 2013-03-12T14:27:59+09:00 Subject: Re: Confusion with empty block printing You are messing up your code because you did not use parentheses, leaving to ruby the task to guess what you wanted to do. On Tue, Mar 12, 2013 at 01:41:09PM +0900, Love U Ruby wrote: > here I am making my confusions are more specific: > > >> print(1..2).class > 1..2=> NilClass > >> print (1..2).class > Range=> nil (print(1..2)).class 1..2=> NilClass print ((1..2).class) Range=> nil > > Why the below 2 produces different output? How does `IRB` read the > above? > > >> print[].class > NoMethodError: undefined method `[]' for nil:NilClass > from (irb):5 > from C:/Ruby193/bin/irb:12:in `
' > >> print [].class > Array=> nil > > How does `IRB` read the above? (print[]).class NoMethodError: undefined method `[]' for nil:NilClass print ([].class) Array=> nil Think about what's going on when you are using or not a space character > > >> print{}.class > => NilClass > >> print {}.class > => NilClass This one is a bit tricker but as you are already be told, the {} is taken as a block and not as a Hash. So both are interpreted in the same way. (print{}).class => NilClass (print {}).class => NilClass Fortunately you won't have to do this often, won't you ? Anyway what you probably wanted to do is this: print({}.class) Hash=> nil > > Why here both lines giving the same outputs unlike the above 2 ? > > -- > Posted via http://www.ruby-forum.com/. >