Hi,
With everything in Ruby being an object, should a string be initialised
as an empty string
e.g.
a_string = ""
or nil
e.g
a_string = nil
cheers
aidy
Hi,
With everything in Ruby being an object, should a string be initialised
as an empty string
e.g.
a_string = ""
or nil
e.g
a_string = nil
cheers
aidy
I prefer using nil that way I can do if statements like
if a_string
...
end
instead of
if a_string.empty?
...
end
The first example is more generic as not all objects respond to .empty?
Farrel
On 11/09/06, aidy <aidy.rutter@gmail.com> wrote:
Hi,
With everything in Ruby being an object, should a string be initialised
as an empty stringe.g.
a_string = ""
or nil
e.g
a_string = nil
cheers
aidy
aidy wrote:
Hi,
With everything in Ruby being an object, should a string be initialised
as an empty stringe.g.
a_string = ""
or nil
e.g
a_string = nil
It depends on the circumstances.
However, an additional note: it may be that you have a misconception about variables in Ruby. Although you named it "a_string" this variable is typeless and it could hold a reference to *any* Ruby object. So a_string "is" not a String as long as it doesn't point to one. And you cannot initialize a String with nil either:
irb(main):003:0> String.new nil
TypeError: can't convert nil into String
from (irb):3:in `initialize'
from (irb):3
irb(main):004:0> String.new "foo"
=> "foo"
HTH
Kind regards
robert
from :0
nil signifies the absence of an object, so you can always set a
variable to nil, regardless of whether the variable was previously
bound to an object of type String, some other type, or indeed nil. I
wonder if what you really want is to impute a logical-false value to
an empty string, to enable expressions like
s = String.new
...
unless s
# here the string is known to be not empty
end
This would violate a fairly deep expectation that the only values in
Ruby which evaluate to logical-false are nil and instances of class
FalseClass (of which there is only one, of course).
On 9/11/06, aidy <aidy.rutter@gmail.com> wrote:
Hi,
With everything in Ruby being an object, should a string be initialised
as an empty stringe.g.
a_string = ""
or nil
e.g
a_string = nil
cheers
aidy
aidy wrote:
Hi,
With everything in Ruby being an object, should a string be initialised
as an empty stringe.g.
a_string = ""
or nil
e.g
a_string = nil
cheers
I would recommend, whenever possible, deferring initialisation
of a String until you actually have said String
Robert Klemme wrote:
aidy wrote:
> Hi,
>
> With everything in Ruby being an object, should a string be initialised
> as an empty string
>
...
However, an additional note: it may be that you have a misconception
about variables in Ruby. Although you named it "a_string" this variable
is typeless and it could hold a reference to *any* Ruby object. So
a_string "is" not a String as long as it doesn't point to one. And you
cannot initialize a String with nil either:
...
+1 Robert
Every thing is an object, but varaibles are references to objects.
If you don't specify an object for a variable to refer to than nil
seems
the best default, yes?
Cheers
<split>
nil signifies the absence of an object, so you can always set a
strange sentence you have written, I am quite surprised,
nil is an object and can thus not signify the absence of an object I am sure
you agree
and yet of course what you say is very much what we all mean most of the
time when we use nil
Look at the following code I use often
x = nil # need to define x outside of blocks I will use it in
# note that I do not use nil to identify falseness or absence I just use
# it because the language foreces me to assign a value to x and nil comes
# into my mind, but somehow I doubt that it is such a good idea
{ ... using x ...}
Why the heck do I not write
x = Object.new
{ ... using x ...}
or just of course
x = 42
{ ... using x }
It is very very easy to slip from definitions to conventions or rather the
other way round.
<valid points snipped>...
This would violate a fairly deep expectation that the only values in
Ruby which evaluate to logical-false are nil and instances of class
FalseClass (of which there is only one, of course).
Again interesting that you kind of have this unorthogonal view, as you could
have written,
are instanecs of the classes NilClass and FalseClass
I wonder for a long time about the concept of having a value indicating the
absence of a value.
I think the conecpt is flawed -useful- but flawed.
Any thaughts?
Cheers
Robert
P.S.
(1) maybe it would be nice to have one singlteon object None or Void or
Omega that has the special property of not having any method and to raise an
UndefinedError whenever read.
Maybe I will put this into my toolbox.
class VoidClass
... undefine all instance methods
def method_missing(*a,&b); raise UndefinedError, "You tried to access an
undefined object"
...
R.
On 9/11/06, Francis Cianfrocca <garbagecat10@gmail.com> wrote:
--
Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui
concerne l'univers, je n'en ai pas acquis la certitude absolue.
- Albert Einstein
ChrisH wrote:
Every thing is an object, but varaibles are references to objects.
If you don't specify an object for a variable to refer to than nil
seems the best default, yes?
Often yes, but this may vary according to circumstances. Since I haven't seen more detail that's about as specific I will get here.
Kind regards
robert
<...>
I wonder for a long time about the concept of having a value indicating the
absence of a value.
I think the conecpt is flawed -useful- but flawed.
<...>
What alternatives do we have? For me it is perfectly natural.
In natural languages we have words which denote nothingness,
why can't we have objects for the same purpose in programming languages?
Regards,
Rimantas
<...>
> I wonder for a long time about the concept of having a value indicating
the
> absence of a value.
> I think the conecpt is flawed -useful- but flawed.
<...>What alternatives do we have?
You read me very well, we might not have any, still I think it is flawed
like as our perception we can put into the modelling process a
programming language is, might be too limited yet.
For me it is perfectly natural.
In natural languages we have words which denote nothingness,
Yep, but see what is behind it,
shall we use nil for
nothingless or defaults or undefined values or place holders or nil, please
do not forget that nil comes from a very specific meaning, but maybe we just
*shall* forget that.
why can't we have objects for the same purpose in programming languages?
objects by all means, we need objects in Ruby there is nothing else, but
maybe nil
is not the object semantically behaving correctly to model e.g.
nothinglessness
c.f. my None implementation above
I feel that if nil models the absence of a value (which it does not quite
accurately) maybe it should not model other things and we should be more
creative ourselfs when using nil.
Maybe we should also have the possibility (as in Python, sorry I said the
word) to define
objects that evaluate to false
like e.g.
x evaluates to false in boolean context if one of the three conditions is
met
(1) x.respond_to?(:I_have_no_idea_maybe_true_value?) && x.true_value? meets
conditions (1???) (2) or (3)
(2) x==false
(3) x.nil?
anyway please note that these are ideas, feelings not concrete ideas and for
sure no rules
Regards,
On 9/12/06, Rimantas Liubertas <rimantas@gmail.com> wrote:
Rimantas
--
http://rimantas.com/
--
Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui
concerne l'univers, je n'en ai pas acquis la certitude absolue.
- Albert Einstein