[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Why is this returning nil?

Dark Ambient

8/9/2006 4:24:00 PM

class User

def initialize(level, status)
@level = level
@status = status
end

def finda
if @status == "active"
print #{@level}
else
puts "user not exist"
end
end
end

user1 = User.new(1, "active")
user2 = User.new(1, "inactive")
user3 = User.new(2, "active")
user4 = User.new(3, "inactive")

user1.finda

Something wrong ..not sure
TIA
Stuart

31 Answers

ts

8/9/2006 4:28:00 PM

0

>>>>> "D" == Dark Ambient <sambient@gmail.com> writes:

D> if @status == "active"
D> print #{@level}

print @level

outside a string, # is a comment :-)

D> else


Guy Decoux

Farrel Lifson

8/9/2006 4:29:00 PM

0

On 09/08/06, Dark Ambient <sambient@gmail.com> wrote:
> class User
>
> def initialize(level, status)
> @level = level
> @status = status
> end
>
> def finda
> if @status == "active"
> print #{@level}
> else
> puts "user not exist"
> end
> end
> end
>
> user1 = User.new(1, "active")
> user2 = User.new(1, "inactive")
> user3 = User.new(2, "active")
> user4 = User.new(3, "inactive")
>
> user1.finda
>
> Something wrong ..not sure
> TIA
> Stuart
>
>

print and puts return nil. As they are the last commands executed
their return statuses are returned.

Farrel

Jeff Schwab

8/9/2006 4:29:00 PM

0

Dark Ambient wrote:
> class User
>
> def initialize(level, status)
> @level = level
> @status = status
> end
>
> def finda
> if @status == "active"
> print #{@level}

Either put the #{} construct in quotes, or don't use it at all. In
other words, use either this:

print "#{@level}\n"

Or this:

puts @level


> else
> puts "user not exist"
> end
> end
> end
>
> user1 = User.new(1, "active")
> user2 = User.new(1, "inactive")
> user3 = User.new(2, "active")
> user4 = User.new(3, "inactive")
>
> user1.finda
>
> Something wrong ..not sure
> TIA
> Stuart
>

Matthew Smillie

8/9/2006 4:31:00 PM

0


On Aug 9, 2006, at 17:23, Dark Ambient wrote:

> class User
>
> def initialize(level, status)
> @level = level
> @status = status
> end
>
> def finda
> if @status == "active"
> print #{@level}
> else
> puts "user not exist"
> end
> end
> end
>
> user1 = User.new(1, "active")
> user2 = User.new(1, "inactive")
> user3 = User.new(2, "active")
> user4 = User.new(3, "inactive")
>
> user1.finda
>
> Something wrong ..not sure
> TIA
> Stuart

Because methods return the value of the last statement. In this
case, both 'puts' and 'print' return nil, the actual output to the
screen is separate from the return value:

print "Hello\n"
Hello
=> nil
puts "Hello"
Hello
=> nil

My guess/advice would be to return the actual string you're
eventually going to output, and deal with the actual output elsewhere
in the code.

matthew smillie.


Alexey Vakhov

8/9/2006 4:31:00 PM

0

Hello!

You forgot quotes in line print #{@level}. Change it to "print
#{@level}" please.

2006/8/9, Dark Ambient <sambient@gmail.com>:
> class User
>
> def initialize(level, status)
> @level = level
> @status = status
> end
>
> def finda
> if @status == "active"
> print #{@level}
> else
> puts "user not exist"
> end
> end
> end
>
> user1 = User.new(1, "active")
> user2 = User.new(1, "inactive")
> user3 = User.new(2, "active")
> user4 = User.new(3, "inactive")
>
> user1.finda
>
> Something wrong ..not sure
> TIA
> Stuart
>
>


--
Alexey Vakhov mailto:vakhov@gmail.com

Alexey Vakhov

8/9/2006 4:36:00 PM

0

Oh. Some mistake. Change it to

print " #{@level}"

of course :)

2006/8/9, Alexey Vakhov <vakhov@gmail.com>:
> Hello!
>
> You forgot quotes in line print #{@level}. Change it to "print
> #{@level}" please.
>
> 2006/8/9, Dark Ambient <sambient@gmail.com>:
> > class User
> >
> > def initialize(level, status)
> > @level = level
> > @status = status
> > end
> >
> > def finda
> > if @status == "active"
> > print #{@level}
> > else
> > puts "user not exist"
> > end
> > end
> > end
> >
> > user1 = User.new(1, "active")
> > user2 = User.new(1, "inactive")
> > user3 = User.new(2, "active")
> > user4 = User.new(3, "inactive")
> >
> > user1.finda
> >
> > Something wrong ..not sure
> > TIA
> > Stuart
> >
> >
>
>
> --
> Alexey Vakhov mailto:vakhov@gmail.com
>


--
Alexey Vakhov mailto:vakhov@gmail.com

Dark Ambient

8/9/2006 4:42:00 PM

0

Wow..lot of answers, thanks to all.
It works and I've learned a few things in the process.

Stuart

On 8/9/06, Alexey Vakhov <vakhov@gmail.com> wrote:
> Hello!
>
> You forgot quotes in line print #{@level}. Change it to "print
> #{@level}" please.
>
> 2006/8/9, Dark Ambient <sambient@gmail.com>:
> > class User
> >
> > def initialize(level, status)
> > @level = level
> > @status = status
> > end
> >
> > def finda
> > if @status == "active"
> > print #{@level}
> > else
> > puts "user not exist"
> > end
> > end
> > end
> >
> > user1 = User.new(1, "active")
> > user2 = User.new(1, "inactive")
> > user3 = User.new(2, "active")
> > user4 = User.new(3, "inactive")
> >
> > user1.finda
> >
> > Something wrong ..not sure
> > TIA
> > Stuart
> >
> >
>
>
> --
> Alexey Vakhov mailto:vakhov@gmail.com
>
>

Irina Rempt

4/24/2009 11:24:00 AM

0

On Friday 24 April 2009 13:08, Nicky wrote:

> Zeborah has worked hard to keep the useful conversations going and put
> off the trolls/politicos/spammers, but there is always a final straw
> and the last few days seem to have provided it.

Piggybacking, because I'm too chicken to post a post of my own (which is
symptomatic): I'm out too. I haven't had the energy to fight, or the
thickness of skin to ignore fighting around me, for a long time. For the
last --well, a year or more-- I've been increasingly afraid to post
anything at all because I was *sure* it would be jumped on, belittled, or
twisted into flamebait.

I don't actively do LJ though I have an account to comment and read locked
posts, but I'm irinarempt on identi.ca, twitter and hyves (www.hyves.nl,
the Dutch equivalent of Facebook), and irina_r on IRC (freenode). Blog in
my .sig.

Goodbye, and thanks for all the fish.

Irina

--
"Of course it is happening inside your head, Harry, but why on earth
should that mean that it is not real?" --Albus Dumbledore
http://www.valdyas.org/foundobjects... Latest: 24-Apr-2009

Bill Swears

4/24/2009 1:47:00 PM

0

gruffstar@googlemail.com wrote:
> On Apr 24, 9:07 am, zebo...@gmail.com (Zeborah) wrote:
>> I've been a member of rasfc since at least 1998; that's a third of my
> /snip/
>
> wow. I've been away for a long time, I guess I've missed what's
> happened to RASFC but I noticed it's much quieter than it used to be
> and most of the regulars no longer seen to be here. I feel
> extraordinarily sad reading this.
>
> Gruff
Many of the older RASFC regulars have moved over to live journal. In
the end, acerbic rhetoric trumped the freedom of an unmoderated forum.
Well, and the fact that on a lot of servers this place is overrun by
spam probably had an impact.

If you follow Zeborah's live journal link, you can probably find the
older regulars you were accustomed to reading.

Unfortunately, the realities of the internet from Alaska and DSL make
live journal unreasonably balky for me to use, unless I want to spend
about $120 a month for my connection (beautiful ridgeline I live on, but
no cable, and only one phone company providing service).

Bill

--
Living on the polemic may be temporarily satisfying, but it will raise
your blood-pressure, and gives you tunnel vision.

Sea Wasp (Ryk E. Spoor)

4/24/2009 2:07:00 PM

0

Bill Swears wrote:
> gruffstar@googlemail.com wrote:
>> On Apr 24, 9:07 am, zebo...@gmail.com (Zeborah) wrote:
>>> I've been a member of rasfc since at least 1998; that's a third of my
>> /snip/
>>
>> wow. I've been away for a long time, I guess I've missed what's
>> happened to RASFC but I noticed it's much quieter than it used to be
>> and most of the regulars no longer seen to be here. I feel
>> extraordinarily sad reading this.
>>
>> Gruff
> Many of the older RASFC regulars have moved over to live journal. In
> the end, acerbic rhetoric trumped the freedom of an unmoderated forum.
> Well, and the fact that on a lot of servers this place is overrun by
> spam probably had an impact.

Well, LJ has its own problems. RaceFail et. al. was in many ways worse
there than I've seen here.

--
Sea Wasp
/^ ;;;
Live Journal: http://seawasp.livej...