Error:- "regex literal in condition"


#1

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


#2

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


#3

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


#4

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.


#5

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
:slight_smile: 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


#6

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
:slight_smile: 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!


#7

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. :wink:

James Edward G. II


#8

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


#9

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. :wink:

James Edward G. II "


#10

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


#11

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


#12

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.


#13

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.


#14

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.


#15

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.


#16

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


#17

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.


#18

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


#19

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


#20

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