error:- "regex literal in condition"

J

John Maclean

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
 
M

Malte Milatz

John Maclean:
while gets
if /Ruby/
print
end
end

You may feel more comfortable writing this:

while line = gets
if line =~ /Ruby/
print line
end
end

And if you want to make sure that the line is read from stdin, you'll want
to write STDIN.gets instead of simply gets, because Kernel#gets looks at the
files specified on the command line and only calls STDIN.gets if there
aren't any.

Malte
 
F

Florian Groß

John said:
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

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
 
Z

Zach

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
 
D

dblack

Hi --

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.

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
(e-mail address removed)

"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.manning.com/books/black
 
D

Daniel Cedilotte

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.
 
J

James Britt

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
 
R

rmagick

Hungarian notation has never been useful except in C code before C
compilers started doing a reliable job of identifying type mismatches,
which was about 20 years ago. Since then there's been no reason for
this practice in C code. It's an ugly and fragile hack that was just
barely justifiable then and is simply execrable now.
 
M

Mike Fletcher

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
 
J

James Edward Gray II

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 Gray II
 
Z

Zach

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 Gray II "

Mike said:
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
 
Z

Zach

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
 
Z

Zach

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
 
J

Jamey Cribbs

Zach said:
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 Cribbs

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.
 
J

Jim McFarland

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.
 
D

Daniel Cedilotte

Zach said:
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


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.
 
L

Logan Capaldo

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 Gray II

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.
 
E

Eero Saynatkari

Zach said:
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


E
 
A

Austin Ziegler

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
 
A

Austin Ziegler

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,298
Messages
2,571,539
Members
48,274
Latest member
HowardKipp

Latest Threads

Top