This can't be right

Hi all,

I’m pretty new to Ruby and RoR but I’m trying to learn for a large
project. I wrote the following method in one of my models but it can’t
be the right way to do this because ruby should be able to do it way
more concisely. Can you tell me of a better way?

def name
if !@name
property = content_properties.find_by_key(‘name’)
if property
@name = property.value
else
@name = “”
end
else
@name
end
end

Thx in advance,

with regards,

Stefan K.

On Thu, Apr 24, 2008 at 9:25 AM, Stefan K.
[email protected] wrote:

  else
    @name = ""
  end
else
  @name
end

end

Here’s one slightly shorter version:

def name
  unless @name
    property = content_properties.find_by_key('name')
    @name = property ? property.value : ""
  end
  @name
end

Hope this helps,

Lyle

property = content_properties.find_by_key(‘name’) unless @name
property ? @name = property.value : @name = “”
@name

Is one way

On Thu, Apr 24, 2008 at 10:25 AM, Stefan K.
[email protected]

end

end

Well, sick:

def name
@name ||= (property = content_properties.find_by_key(‘name’) &&
property.name) || “”
end

mfg, simon … untried

Well, sick:

def name
@name ||= (property = content_properties.find_by_key(‘name’) &&
property.name) || “”
end

Well, helper variables aren’t “elegant”:

def name
@name ||= (content_properties.find_by_key(‘name’) ||
Struct.new(:name).new(‘’)).name
end

mfg, simon … trying’s lame

Simon K. [email protected] writes:

Well, helper variables aren’t “elegant”:

def name
@name ||= (content_properties.find_by_key(‘name’) ||
Struct.new(:name).new(‘’)).name
end

That’s basically equivalent to:

,----
| def name
| @name ||= content_properties.find_or_initialize_by_key(‘name’).name.to_s
| end
`----

or even this flame bait:

,----
| def name
| @name ||= content_properties.find_by_key(‘name’).name rescue “”
| end
`----

Of course, I prefer the former.

On Thu, Apr 24, 2008 at 9:25 PM, Peter J. [email protected] wrote:

,----
| end
`----

Of course, I prefer the former.
I do not, because the former will not work when find_or_etc.etc. will
return nil
Next we shall change the message from #name to #value and that done I
have no reason at all to flame ( there is no such thing on this list
:wink: you at all, I quite like the rescue, it is probable the most
readable solution for my eyes.

I see yet another alternative which is maybe not the most pretty code,
but maybe the easiest to deal with (debugging, evolution of the code)

@name ||= content_properties.find…(“name”)
@name &&= @name.value
@name ||= “”

just a completely different style.
Cheers
Robert


Peter J. [pjones at domain below]
pmade inc. - http://pmade.com


http://ruby-smalltalk.blogspot.com/


Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

On Thu, 2008-04-24 at 23:25 +0900, Stefan K. wrote:

end

end

Just eyeballing this:

def name
@name ||= ( property = content_properties.find_by_key(‘name’) ?
property.value : “” )
end

This isn’t tested (because I’m not planning on implementing
content_properties and find_by_key and so on) so it may not work out of
the box, but it should give you and idea of what to shoot for. I did
test this on @name ||= ( a = nil ? a.value : “” ) as a sanity check and
got the expected “”.

I will try the last one first:

@name ||= ( property = content_properties.find_by_key(‘name’) ?
property.value : “” )

Very short and very readable, all cases are in there nicely

Thx!

I see yet another alternative which is maybe not the most pretty code,
but maybe the easiest to deal with (debugging, evolution of the code)

@name ||= content_properties.find…(“name”)
@name &&= @name.value
@name ||= “”

When @name is a String before that, first line does nothing, second line
throws an exception.

mfg, simon … l

On Fri, Apr 25, 2008 at 11:35 AM, Simon K. [email protected] wrote:

Guess I got it wrong :wink:

Well this is not an example for this coding style, sorry.
R.


http://ruby-smalltalk.blogspot.com/


Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein