Am Freitag, 30. Okt 2009, 22:45:10 +0900 schrieb Aldric G.:
Bertram S. wrote:
My question was not how to implement it but whynot to add
it to the interpreter.
[yet another implementation]
Why wouldn’t this be implemented by default, er, I don’t know, I really
can’t think of a time when I’d need this type of behavior.
That’s just about you but not about other Ruby programmers.
Bertram
shrug
Hey, I gave you an implementation. You want it, use it. You don’t want
it, make your own. Ruby’s flexible. Because some behavior is not there
by default doesn’t mean it’s a bias.
Am Samstag, 31. Okt 2009, 01:46:10 +0900 schrieb Aldric G.:
Bertram S. wrote:
String#notempty?
Because some behavior is not there by default doesn’t mean it’s
a bias.
That’s what I wanted to say: I am convinced it should be there by
default. It’s just a few lines of code. It’s pure logic when you
have Numeric#nonzero? and it’s very useful to build default
values:
str.notempty? || “(none)” # analoigous to SQL-coalesce()
I guess it would be more useful than flip-flops or class variables.
I don’t even know if this was mentioned in the thread before or not, but
hey, there it is.
My apologies - this IRB session was a couple of days old, and I have
some Rails libraries loaded… This is, I think, part of ActiveSupport or
ActiveRecord.
It’s just a few lines of code. It’s pure logic when you
have Numeric#nonzero? and it’s very useful to build default
values:
str.notempty? || “(none)” # analoigous to SQL-coalesce()
I guess it would be more useful than flip-flops or class variables.
Bertram
Bertram,
So do like everyone else and extend the language to your liking
I have a small custom Rails app here which has the following in an
Integer.rb file, under /lib :
class Integer
def to_hours
self / 3600
end
end
Because I needed that (yes - just like that, no floating point division)
and it wasn’t in Ruby.
Am Samstag, 31. Okt 2009, 01:46:10 +0900 schrieb Aldric G.:
Bertram S. wrote:
String#notempty?
Because some behavior is not there by default doesn’t mean it’s
a bias.
That’s what I wanted to say: I am convinced it should be there by
default. It’s just a few lines of code. It’s pure logic when you
have Numeric#nonzero? and it’s very useful to build default
values:
str.notempty? || “(none)” # analoigous to SQL-coalesce()
It’s trivial to build:
class String
def notempty?
empty? ? nil : self
end
end
I guess it would be more useful than flip-flops or class variables.
Am Samstag, 31. Okt 2009, 01:54:10 +0900 schrieb Aldric G.:
Aldric G. wrote:
“123456”.last 4
I don’t even know if this was mentioned in the thread before or not, but
hey, there it is.
My apologies - this IRB session was a couple of days old, and I have
some Rails libraries loaded… This is, I think, part of ActiveSupport or
ActiveRecord.
This proves that there is need for it :-)))))
scnr.
time. Don’t you need to save time?
Really? You’re arguing about the efficiency of something that is about
6.8µs slower?
I don’t personally need to save the time. Nor do you, unless you have
a real-world program using this method that you have benchmarked and
found this particular method call to be a significant portion of your
execution time.
Your calculation needs more than four times as much processor
time. Don’t you need to save time?
Really? You’re arguing about the efficiency of something that is about
6.8µs slower?
You took that personally, really?
Of course not. My point was that too often people get focused on
premature optimization. Just because one method is “four times” slower
than another does not mean that one should necessarily be chosen over
the other. My rule of thumb is:
Choose the right algorithms.
Choose the implementation most convenient for me.
If speed is an issue, benchmark your program and optimize the most
painful parts first.
Bertram’s argument and implementation are only important, I’m saying,
to someone who has written a program that needs to process millions of
tail endings of strings in a time-critical application, and who has
proven that the speed of String#[] is a significant portion of that
time. (Important only to them, but perhaps interesting to more.)
string[index, string.size - index]
I don’t think there’s anything wrong with it. It works well, and
there’s nothing stylistically wrong with using a local variable twice.
If index is a method that does a (re)calculation every time, you’d
want to cache it, but that’s not the case in the example.
There's nothing wrong with it other than it doesn't work. As others
have pointed out, it’s:
string[-index…-1]
However, I'm more concerned with your general attitude about
language
structure. Even if it did work, you can say the same thing for my
original
solution:
string[index, string.size - index]
It works well... except that it uses both index _and_ string
variables
twice.
Am Freitag is right. It would be better if there were some method
of
getting the tail end of a string by only mentioning the needed
parameters
once. I’m very surprised that Ruby does not have this level of
expressiveness…
On Oct 31, 10:35 pm, “Just Another Victim of the Ambient M.” [email protected] wrote:
[…] As others have pointed out, it’s:
string[-index…-1]
[…] It would be better if there were some method of
getting the tail end of a string by only mentioning the needed parameters
once. I’m very surprised that Ruby does not have this level of
expressiveness…
Is what I’ve done above a misquote? (Both these statements seem
attributed to you in the same message on comp.lang.ruby.)
Assuming it’s not a misquote: how is the code you wrote on the second
quoted line above not exactly what you’re “surprised that ruby does
not have”?
[…] As others have pointed out, it’s:
string[-index…-1]
[…] It would be better if there were some method of
getting the tail end of a string by only mentioning the needed parameters
once. I’m very surprised that Ruby does not have this level of
expressiveness…
Is what I’ve done above a misquote? (Both these statements seem
attributed to you in the same message on comp.lang.ruby.)
Assuming it’s not a misquote: how is the code you wrote on the second
quoted line above not exactly what you’re “surprised that ruby does
not have”?
I'm sorry, my client is not quoting this post for some no good
reason…
You haven't misquoted, it was simply my mistake. I was conflating
two
different posts and while I was revising this one, I accidentally
included
this portion which, as you’ve pointed out, is no longer valid…
That’s what I wanted to say: I am convinced it should be there by
default. It’s just a few lines of code. It’s pure logic when you
have Numeric#nonzero? and it’s very useful to build default
values:
str.notempty? || “(none)” # analoigous to SQL-coalesce()
So basically “String#notempty?” would be the same thing as
“String#any?” ?
Your calculation needs more than four times as much processor
time. Don’t you need to save time?
Really? You’re arguing about the efficiency of something that is about
6.8µs slower?
You took that personally, really?
I don’t personally need to save the time. Nor do you, unless you have
a real-world program using this method that you have benchmarked and
found this particular method call to be a significant portion of your
execution time.
That’s what I wanted to say: I am convinced it should be there by
default. It’s just a few lines of code. It’s pure logic when you
have Numeric#nonzero? and it’s very useful to build default
values:
 str.notempty? || “(none)”   # analoigous to SQL-coalesce()
So basically “String#notempty?” would be the same thing as
“String#any?” ?
Strings aren’t Enumerable in 1.9, so there’s no String#any?. You can
do string.chars.any? or string[0] or !string.empty? to test for
contents.
David
–
The Ruby training with D. Black, G. Brown, J.McAnally
Compleat Jan 22-23, 2010, Tampa, FL
Rubyist http://www.thecompleatrubyist.com