Forum: Ruby error:- "regex literal in condition"

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
John M. (Guest)
on 2006-01-20 20:12
(Received via mailing list)
Hi Guys,

Simple script that spits out "Ruby" if it was typed at stdin. What's the
error, see below.
#!/usr/bin/env ruby

while gets
  if /Ruby/
    print
  end
end


jayeola@tp20$ ruby fx/ruby/p22
fx/ruby/p22:4: warning: regex literal in condition
haha
Ruby
Ruby
Florian GroÃ? (Guest)
on 2006-01-20 20:45
(Received via mailing list)
John M. wrote:

> fx/ruby/p22:4: warning: regex literal in condition
Just what Ruby says: The above ought to be the same as this:

re = /Ruby/
while gets
   if re
     print
   end
end

It's just if obj and if obj executes the code whenever it is not false
or nil.

The correct way to do this:

while line = gets
   if /Ruby/.match(line) then
     print line
   end
end
Zach (Guest)
on 2006-01-20 21:00
(Received via mailing list)
This is an ignorant question to pose, but here I go anyway.

Does anyone see advantages to using Hungarian Case to variable names in
Ruby, or do you think it may hinder as some people call "accidental
abstraction" [Bill Davis] ? Since Ruby is big with "Duck Typing", would
using Hungarian Case be more of a verbal obstacle than a more
descriptive variable. What about varying degrees, such as for integers,
doubles, strings, and o for Objects, but not exhaustive?

(For those who don't know about Hungarian typing, it would similar to an
integer you would name "counter" being named "iCounter" or a variable
you would normally name "duck" be "oDuck" because it is an object)

I know this is a rather old topic to be bringing into a newer
technology, but I'm curious on what your all's take is on this.

-Zach
unknown (Guest)
on 2006-01-20 21:12
(Received via mailing list)
Hi --

On Sat, 21 Jan 2006, Zach wrote:

> integer you would name "counter" being named "iCounter" or a variable
> you would normally name "duck" be "oDuck" because it is an object)
>
> I know this is a rather old topic to be bringing into a newer
> technology, but I'm curious on what your all's take is on this.

Tagging variables based on the class of the objects to which they
refer seems to me to be aggressively anti-duck-typing.  It's also ugly
:-)  I think I'll stick with the traditional style.


David

--
David A. Black
removed_email_address@domain.invalid

"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.manning.com/books/black
Daniel C. (Guest)
on 2006-01-20 21:21
(Received via mailing list)
> Does anyone see advantages to using Hungarian Case to variable names in
> Ruby, or do you think it may hinder as some people call "accidental
> abstraction" [Bill Davis] ? Since Ruby is big with "Duck Typing", would
> using Hungarian Case be more of a verbal obstacle than a more
> descriptive variable. What about varying degrees, such as for integers,
> doubles, strings, and o for Objects, but not exhaustive?

IMHO, I've never seen the use for it. As far as I know, variable names
should be able to tell you what it holds at a glance. I am however of
the old school of programming and hence, might not be the best person to
give my opinion but none the less, I still find that Hungarian Notation
is a big waste of time.
James B. (Guest)
on 2006-01-20 21:30
(Received via mailing list)
removed_email_address@domain.invalid wrote:

>
> Tagging variables based on the class of the objects to which they
> refer seems to me to be aggressively anti-duck-typing.  It's also ugly
> :-)  I think I'll stick with the traditional style.
>

Class and object names should be clear enough to reflect what that are
and what they are for.  Ideally no one is still trying to conserve space
or finger power by naming things 'c' or 'x' or 'ssn' except for very
targeted use (e.g., short-scoped disposable values).

If you have a variable that refers, say, to an instance of a class
representing a hotel reservation, name it as such:
current_hotel_reservation.  Not hr_curr or the like.

James


--

http://www.ruby-doc.org       - Ruby Help & Documentation
http://www.artima.com/rubycs/ - The Journal By & For Rubyists
http://www.rubystuff.com      - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com     - Playing with Better Toys
http://www.30secondrule.com   - Building Better Tools
Mike F. (Guest)
on 2006-01-20 21:44
Zach wrote:
[...]
> Does anyone see advantages to using Hungarian Case to variable names in
> Ruby, or do you think it may hinder as some people call "accidental
> abstraction" [Bill Davis] ? Since Ruby is big with "Duck Typing", would
> using Hungarian Case be more of a verbal obstacle than a more
> descriptive variable. What about varying degrees, such as for integers,
> doubles, strings, and o for Objects, but not exhaustive?
>
> (For those who don't know about Hungarian typing, it would similar to an
> integer you would name "counter" being named "iCounter" or a variable
> you would normally name "duck" be "oDuck" because it is an object)

See this article on what Hungarian notation really was meant to help
with (it was meant to provide semantic type (this value is used for X)
not data type (this value is an int)).

http://www.joelonsoftware.com/articles/Wrong.html
James G. (Guest)
on 2006-01-20 21:51
(Received via mailing list)
On Jan 20, 2006, at 12:21 PM, Zach wrote:

> Does anyone see advantages to using Hungarian Case to variable
> names in
> Ruby, or do you think it may hinder as some people call "accidental
> abstraction" [Bill Davis] ?

I assume you know this, but just to be sure, Joel on Software has a
semi-famous article about the virtues of Hungarian Notation:

http://www.joelonsoftware.com/articles/Wrong.html

If you make it all the way to the end of that, it talks about how the
notation is intended to show a "kind", not "type".  When used that
way, I can't decide if it interferes with Duck Typing or not...  A
great example is escaping me.

It may indeed be useful, but it is hard to argue that it isn't ugly.  ;)

James Edward G. II
Zach (Guest)
on 2006-01-20 21:54
(Received via mailing list)
Actually I hadn't read this article, but I've heard of Joel. Thanks to
the both of you for pointing it out.

-Zach

"I assume you know this, but just to be sure, Joel on Software has a
semi-famous article about the virtues of Hungarian Notation:

http://www.joelonsoftware.com/articles/Wrong.html

If you make it all the way to the end of that, it talks about how the
notation is intended to show a "kind", not "type".  When used that  way,
I can't decide if it interferes with Duck Typing or not...  A  great
example is escaping me.

It may indeed be useful, but it is hard to argue that it isn't ugly.  ;)

James Edward G. II "
Zach (Guest)
on 2006-01-20 22:10
(Received via mailing list)
Alrighty,

    I've another question I'd like to pose the Ruby community. Coming
from a Java background, so I'm rather used to "camelCase" as opposed to
what looks to be the standard "ruby_case". (For those who don't know, If
a method is java was named "DoSomething"  it would be called
"doSomething" in Java, and likewise "do_something" in Ruby.)

Arguments I've personally witnessed against Camel Case is Acronyms. If I
have an "ABC" in Camel Case it I'd have to would be to break up the
acronym like "aBC" if it appears at the beginning of the word. A lot of
people try to push the word to the end, others say to have the whole
acronym as lowercase "abc", but I think that is just a workaround for
the problem not a solution.

Once I started programming in ruby, I was a little surprised at the use
of the underscore, but I'm wondering the sentiments of the programmers
out there. Do you prefer word breaks by underscore or case? Does is look
more readable? Does anything else irk you with Camel Case and/or Ruby's
preference?

Not trying to incite flame_wars or the like, looking for honest
opinions.

-Zach
Zach (Guest)
on 2006-01-20 22:13
(Received via mailing list)
Aha,

    I thought there was another issue. Getters/Setters like getBlah(),
setBlah(). If it's getABC() or setABC(), frameworks like Java Server
Faces require you to just use the relevant information, so you end up
entering. "obj.aBC".

-Zach
Jamey C. (Guest)
on 2006-01-20 22:19
(Received via mailing list)
Zach wrote:

> Once I started programming in ruby, I was a little surprised at the
> use of the underscore, but I'm wondering the sentiments of the
> programmers out there. Do you prefer word breaks by underscore or
> case? Does is look more readable? Does anything else irk you with
> Camel Case and/or Ruby's preference?

I used to do camelCase, mainly when I programmed mostly in Python.  A
while ago I switched over to ruby_case, and I prefer it over camelCase.
To me, the underscore just makes method names and variable names easier
to read.

HTH,

Jamey C.

Confidentiality Notice: This email message, including any attachments,
is for the sole use of the intended recipient(s) and may contain
confidential and/or privileged information. If you are not the intended
recipient(s), you are hereby notified that any dissemination,
unauthorized review, use, disclosure or distribution of this email and
any materials contained in any attachments is prohibited. If you receive
this message in error, or are not the intended recipient(s), please
immediately notify the sender by email and destroy all copies of the
original message, including attachments.
Christian N. (Guest)
on 2006-01-20 22:22
(Received via mailing list)
Zach <removed_email_address@domain.invalid> writes:

> Once I started programming in ruby, I was a little surprised at the
> use of the underscore, but I'm wondering the sentiments of the
> programmers out there. Do you prefer word breaks by underscore or
> case? Does is look more readable? Does anything else irk you with
> Camel Case and/or Ruby's preference?
>
> Not trying to incite flame_wars or the like, looking for honest opinions.

Under_scoring is the best thing after using-hyphens, which is not
possible in Ruby without dirty tricks.
Jim McFarland (Guest)
on 2006-01-20 23:10
(Received via mailing list)
Well, I started out as a C programmer many years ago, and after
several other languages, I ended up doing Java for much of the last 5
years and adopted the Sun Java coding conventions.  When I started
learning Ruby, its idioms, and its coding conventions, I was very
happy to see "ruby_case" which is what I did in C as far back as 16
years ago.
Daniel C. (Guest)
on 2006-01-20 23:13
(Received via mailing list)
Zach wrote:

> acronym like "aBC" if it appears at the beginning of the word. A lot
> Not trying to incite flame_wars or the like, looking for honest opinions.
>
> -Zach


I use a somewhat modified version of the camelCase to fit my needs. It's
not that I don't like other methods, I've just been used to this one and
that's it. I'm sure other methods are just as good, but the thing is, no
matter what language I use, my styling is always the same. Even when I'm
web programming. It's just a matter of pref, as usual.
Logan C. (Guest)
on 2006-01-20 23:13
(Received via mailing list)
On Jan 20, 2006, at 2:13 PM, James Edward G. II wrote:

> http://www.joelonsoftware.com/articles/Wrong.html
>
>

As I was reading this, all I kept thinking of was ML/Haskell. In
pseudo ML:

type unsafe_string = UString of string
type safe_string = SString of string

Request :: string -> unsafe_string
Encode :: unsafe_string -> safe_string
Write :: safe_string -> unit

etc..

Now the compiler is tracking all this junk for you. I realize this is
the land of duck-typing, but I think if the type-inference is good
enough, you can have your cake and eat it too.
Eero S. (Guest)
on 2006-01-20 23:34
Zach wrote:
> Alrighty,
>
>     I've another question I'd like to pose the Ruby community. Coming
> from a Java background, so I'm rather used to "camelCase" as opposed to
> what looks to be the standard "ruby_case". (For those who don't know, If
> a method is java was named "DoSomething"  it would be called
> "doSomething" in Java, and likewise "do_something" in Ruby.)
>
> Arguments I've personally witnessed against Camel Case is Acronyms. If I
> have an "ABC" in Camel Case it I'd have to would be to break up the
> acronym like "aBC" if it appears at the beginning of the word. A lot of
> people try to push the word to the end, others say to have the whole
> acronym as lowercase "abc", but I think that is just a workaround for
> the problem not a solution.
>
> Once I started programming in ruby, I was a little surprised at the use
> of the underscore, but I'm wondering the sentiments of the programmers
> out there. Do you prefer word breaks by underscore or case? Does is look
> more readable? Does anything else irk you with Camel Case and/or Ruby's
> preference?
>
> Not trying to incite flame_wars or the like, looking for honest
> opinions.

The big thing is that Ruby attaches semantic meaning to the naming
scheme: all constants must start with an uppercase letter, so your
classes and modules have CamelCaseNames. Because of this, seeing
something like obj.MethodName or even obj.methodName causes a ruby
programmer an involuntary double-take to ensure it indeed refers to
a method, not a constant. For this reason (and others that have been
battled over pretty much since the keyboard was invented), it is
preferred that the following is used:

  * Classes and modules use CamelCase
  * Constants use ALL_CAPS
  * Methods and variables use underscored_names

> -Zach


E
Austin Z. (Guest)
on 2006-01-20 23:40
(Received via mailing list)
On 20/01/06, Zach <removed_email_address@domain.invalid> wrote:
> This is an ignorant question to pose, but here I go anyway.

> Does anyone see advantages to using Hungarian Case to variable names
> in Ruby, or do you think it may hinder as some people call "accidental
> abstraction" [Bill Davis] ? Since Ruby is big with "Duck Typing",
> would using Hungarian Case be more of a verbal obstacle than a more
> descriptive variable. What about varying degrees, such as for
> integers, doubles, strings, and o for Objects, but not exhaustive?

> (For those who don't know about Hungarian typing, it would similar to
> an integer you would name "counter" being named "iCounter" or a
> variable you would normally name "duck" be "oDuck" because it is an
> object)

> I know this is a rather old topic to be bringing into a newer
> technology, but I'm curious on what your all's take is on this.

The term is actually Apps Hungarian and System Hungarian naming. What
you show (iCounter) is System Hungarian and is not what the originator
of Hungarian naming wanted, promulgated improperly through (primarily)
Charles Petzold and Microsoft MSDN documentation.

More useful would be something like "cbCounter" which means "count of
bytes called Counter". It encodes the purpose of the variable instead of
the type of the variables. However, in Ruby, it'd probably be more
idiomatic to simply use "byte_counter".

-austin
Austin Z. (Guest)
on 2006-01-21 00:01
(Received via mailing list)
On 20/01/06, Zach <removed_email_address@domain.invalid> wrote:
> Once I started programming in ruby, I was a little surprised at the use
> of the underscore, but I'm wondering the sentiments of the programmers
> out there. Do you prefer word breaks by underscore or case? Does is look
> more readable? Does anything else irk you with Camel Case and/or Ruby's
> preference?

Japanese readers find the underscore more readable. I prefer
snake_case, personally.

-austin
Zach (Guest)
on 2006-01-21 00:32
(Received via mailing list)
I know the current naming standards of the language. My curiosity is
how many programmers prefer it.

Prime example given by a few: snake_case is preferable to Japanese
Readers.

-Zach
Gabe B. (Guest)
on 2006-01-21 03:12
(Received via mailing list)
I tend to prefer to adopt the stylistic conventions of the language I am
working in. Rather than try to hold on to some cross-language personal
preference, I find having my code match the style of the libraries that
I am
using to be the most aesthetically pleasing.

Gabe
Bill K. (Guest)
on 2006-01-21 03:18
(Received via mailing list)
From: "Zach" <removed_email_address@domain.invalid>
>
>    I know the current naming standards of the language. My curiosity is
> how many programmers prefer it.

Personally, I like Ruby's naming standards.  I prefer snake_case to
camelCase in general, so that helps.  But an important point is, I
think,
that it's better to adopt the naming standards of whatever language
I'm coding in.  In Java, I follow Sun's naming conventions.  We've seen
posts from Ruby programmers who stick with their own style, regardless
of language.  That's their prerogative, but if one is writing library
code
to share with the Ruby community, I think it's especially important to
be
willing to adopt the Ruby naming conventions.  It's always a sinking
feeling (for me anyway) to download what might be a nice open source
Ruby library, and find the author was either unaware of the Ruby
naming conventions, or decided to disregard them, thereby putting
unnecessary burden on all users of the library.  Makes me want to
scream, "Oh! You inconsiderate miserable b****rd!!!!" - but that is
always tempered by, "But, thanks for the free code!"  <grin>

Anyway, "When in Rome, ..." etc.


Regards,

Bill
Gregory B. (Guest)
on 2006-01-23 05:16
(Received via mailing list)
On 1/20/06, Bill K. <removed_email_address@domain.invalid> wrote:
> It's always a sinking
> feeling (for me anyway) to download what might be a nice open source
> Ruby library, and find the author was either unaware of the Ruby
> naming conventions, or decided to disregard them, thereby putting
> unnecessary burden on all users of the library.  Makes me want to
> scream, "Oh! You inconsiderate miserable b****rd!!!!" - but that is
> always tempered by, "But, thanks for the free code!"  <grin>

Nicely put!  I too prefer to use the conventions of the language I am
working in.
I used to use camelCase everywhere when I first got into Java.  That
angered a few people.  I learned ;)
Mark Probert (Guest)
on 2006-01-23 20:03
(Received via mailing list)
hi ..

James B. wrote:
>
> If you have a variable that refers, say, to an instance of a class
> representing a hotel reservation, name it as such:
> current_hotel_reservation.  Not hr_curr or the like.
>
Personally, I think that there is a place for short names as I find them
much more readable.  Verbosity can be a real impediment to readability.

--

-mark.
James B. (Guest)
on 2006-01-23 20:12
(Received via mailing list)
Mark Probert wrote:
> much more readable.  Verbosity can be a real impediment to readability.
>

Well, readability and communication are key.

James
Kevin O. (Guest)
on 2006-01-23 20:48
James B. wrote:
> Mark Probert wrote:
>> much more readable.  Verbosity can be a real impediment to readability.
>>
>
> Well, readability and communication are key.
>
> James

An interesting concept for functional apps (or system) Hungarian in
Ruby.  It would be pretty easy to set up the missing_method function to
extract out certain tags from a variable name.

For example,
assume you have a model with an attribute called 'Text'

you could set up missing_method to pick up 'iText' and return
'Text.to_i' or 'hText' could return 'h(Text)' etc...

NOTE: I'm not advocating that this is a good idea, I'm just pointing out
that you could use Hungarian notation in Ruby and actually have it do
something.

_Kevin
unknown (Guest)
on 2006-01-23 22:29
(Received via mailing list)
Hi --

On Tue, 24 Jan 2006, Kevin O. wrote:

> Ruby.  It would be pretty easy to set up the missing_method function to
> extract out certain tags from a variable name.
>
> For example,
> assume you have a model with an attribute called 'Text'

(A class with an attribute called "text"?)

> you could set up missing_method to pick up 'iText' and return
> 'Text.to_i' or 'hText' could return 'h(Text)' etc...

Why not just call to_i or h in the first place? :-)


David

--
David A. Black
removed_email_address@domain.invalid

"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.manning.com/books/black
Kevin O. (Guest)
on 2006-01-23 22:47
Kevin O. wrote:

> NOTE: I'm not advocating that this is a good idea, I'm just pointing out
> that you could use Hungarian notation in Ruby and actually have it do
> something.

David A. Black said..

> Why not just call to_i or h in the first place? :-)

In most cases I expect that it would be better that way, and that would
be my preference as well.  I suppose it might be useful for porting
code.

 _Kevin
James G. (Guest)
on 2006-01-23 22:54
(Received via mailing list)
On Jan 23, 2006, at 12:49 PM, Kevin O. wrote:

> An interesting concept for functional apps (or system) Hungarian in
> Ruby.  It would be pretty easy to set up the missing_method
> function to
> extract out certain tags from a variable name.
>
> For example,
> assume you have a model with an attribute called 'Text'
>
> you could set up missing_method to pick up 'iText' and return
> 'Text.to_i' or 'hText' could return 'h(Text)' etc...

How are you going to assign a variable (iText = ...), and then
convince Ruby to treat it as a method (to trigger method_missing())?

James Edward G. II
James B. (Guest)
on 2006-01-23 23:12
(Received via mailing list)
removed_email_address@domain.invalid wrote:
> Hi --
>
> On Tue, 24 Jan 2006, Kevin O. wrote:
...

>
> (A class with an attribute called "text"?)
>
>> you could set up missing_method to pick up 'iText' and return
>> 'Text.to_i' or 'hText' could return 'h(Text)' etc...
>
>
> Why not just call to_i or h in the first place? :-)
>

Aesthetics?  Encapsulation? Hiding implementation details?  Amusement?

I've seen code that takes find_by_foo( bar ) and converts it into find(
:foo => bar ) or some such thing.  Is this to save typing (he asked
rhetorically)?  Expression of intent?  Slickitude factor ("Ha! Try THAT,
Java droids!" )?  (Still rhetorical; please, no flames.)

I showed this as part of  a Ruby demo, and at least one person didn't
see what that bought you.  Fair enough.  I tend to like it, though.

hText (though h_text or text_h looks more Rubyish) is just another
example of embedding code in message names.

But it can teeter toward DSL: domain-specific logorrhea.

foo.baz_then_bar_unless_bif_equals_47

James B.

--

http://www.ruby-doc.org       - Ruby Help & Documentation
http://www.artima.com/rubycs/ - The Journal By & For Rubyists
http://www.rubystuff.com      - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com     - Playing with Better Toys
http://www.30secondrule.com   - Building Better Tools
Kevin O. (Guest)
on 2006-01-23 23:20
James G. wrote:

> How are you going to assign a variable (iText = ...), and then
> convince Ruby to treat it as a method (to trigger method_missing())?
>

I just tested this with a quick one and found that if you do something
like

object.iTest=5

Then method_missing will get 'iTest=' as the symbol, and 'n' as an
argument.  Then it's a simple matter of trimming the 'i' off the front
and returning the result of '(test=n).to_i'.

_Kevin
James G. (Guest)
on 2006-01-23 23:31
(Received via mailing list)
On Jan 23, 2006, at 3:20 PM, Kevin O. wrote:

>
> Then method_missing will get 'iTest=' as the symbol, and 'n' as an
> argument.  Then it's a simple matter of trimming the 'i' off the front
> and returning the result of '(test=n).to_i'.

Gotcha.  I thought you were talking about using local variables.  I
understand now.  Thanks.

James Edward G. II
This topic is locked and can not be replied to.