[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

This can't be right

Stefan Kroes

4/24/2008 2:26:00 PM

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 Kroes
--
Posted via http://www.ruby-....

10 Answers

Phlip

4/24/2008 2:43:00 PM

0

Stefan Kroes wrote:
> 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?

Firstly, write unit tests covering each of its branches. That way, you can do
the edits I'm about to do, but one at a time, and pass all the tests after each one.

> def name
> if !@name
> property = content_properties.find_by_key('name')
> if property
> @name = property.value
> else
> @name = ""
> end
> else
> @name
> end
> end

Good design is about merging redundant things. Your @name appears too many
times, so we will have a crack at that.

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

Yes, you can put a linefeed (or a ; ) inside parens. They return their last
statement's value, just like a function returns its last statement's value.

Next, ||= is a common idiom for "surprise assignment". If the left argument is a
nil or false variable, the right side is evaluated and assigned.

And, finally, @name is nil if it's not declared yet.

--
Phlip

Lyle Johnson

4/24/2008 2:54:00 PM

0

On Thu, Apr 24, 2008 at 9:25 AM, Stefan Kroes
<s.a.kroes@student.utwente.nl> wrote:

> 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

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

Zundra Daniel

4/24/2008 2:56:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

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 Kroes <s.a.kroes@student.utwente.nl>
wrote:

> 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 Kroes
> --
> Posted via http://www.ruby-....
>
>

Simon Krahnke

4/24/2008 3:24:00 PM

0

* Stefan Kroes <s.a.kroes@student.utwente.nl> (16:25) schrieb:

> def name
> if !@name
> property = content_properties.find_by_key('name')
> if property
> @name = property.value
> else
> @name = ""
> end
> else
> @name
> end
> end

Well, sick:

def name
@name ||= (property = content_properties.find_by_key('name') &&
property.name) || ""
end

mfg, simon .... untried

Simon Krahnke

4/24/2008 4:39:00 PM

0

* Simon Krahnke <overlord@gmx.li> (17:23) schrieb:

> 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

Peter Jones

4/24/2008 7:21:00 PM

0

Simon Krahnke <overlord@gmx.li> 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.

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

Robert Dober

4/24/2008 9:01:00 PM

0

On Thu, Apr 24, 2008 at 9:25 PM, Peter Jones <dev-null@pmade.com> wrote:
> Simon Krahnke <overlord@gmx.li> 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.
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
;) 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 Jones [pjones at domain below]
> pmade inc. - http:/...
>
>



--
http://ruby-smalltalk.blo...

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

Stefan Kroes

4/25/2008 8:05:00 AM

0

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!
--
Posted via http://www.ruby-....

Simon Krahnke

4/25/2008 9:29:00 AM

0

* Robert Dober <robert.dober@gmail.com> (23:00) schrieb:

> 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

Robert Dober

4/25/2008 12:37:00 PM

0

On Fri, Apr 25, 2008 at 11:35 AM, Simon Krahnke <overlord@gmx.li> wrote:
> * Robert Dober <robert.dober@gmail.com> (23:00) schrieb:


Guess I got it wrong ;)

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




--
http://ruby-smalltalk.blo...

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