Elseif v. elsif?

7stud 7stud schrieb:

            end
    end

end
if FILE == $0
mg = MegaGreeter.new([“Sally”, “Jane”, “Bob”])
mg.say_hi
end

Hi 7stud,

when reading (compiling) your code, Ruby can only detect syntactic
errors, and your code is syntactically correct. Other errors can only be
determined at run time. Let me show you…

You get an error when you try your code with another instance:

mg2 = MegaGreeter.new(nil)
mg2.say_hi

Output:


in say_hi': undefined methodelseif’ (NoMethodError)

This error message shows that Ruby tried to execute the method #elseif,
which obviously isn’t defined. Ruby cannot decide in advance whether
there will be an error or not:

mg3 = MegaGreeter.new(nil)

def mg3.elseif(arg)
puts “Hello from elseif with arg #{arg}”
end

mg3.say_hi

This code defines the method #elseif for the object mg3. The output is:


Hello from elseif with arg false
in say_hi': undefined methodeach’ for nil:NilClass (NoMethodError)

You can see that the #elseif method is called, but then there’s an error
in the next line: because @names is nil in this case, we call the method
#each on the object nil, which isn’t defined.

I should have said: normally it isn’t defined. We can change this:

def nil.each
yield “nil”
end

mg3.say_hi

This code defines the method #each for the object nil, so that it passes
the string “nil” to the block. The output is:


Hello from elseif with arg true
Hello nil!

You can see that your code executes fine in this case. This can only be
determined by actually running the code. Ruby is very dynamic, which
sometimes isn’t an advantage, as has been in your case. But it can be a
very powerful tool, which is why we all hang out here.

Regards,
Pit

Add some spaces at the front of the ‘elseif’ line, so that it aligns
with
the ‘puts “…”’ on the line above. Then you’ll see how Ruby is
interpreting
your code.

Unlike python, Ruby doesn’t force you to align your source in a
particular
way.

Ok, thanks.

Thanks for all the help. It looks like way up at the top, Robert D.
first identified the problem, but I didn’t see how it applied.

As a newcomer, I would suggest someone redo the tutorial “Ruby in 20
Minutes”: add in ‘then’ and include a statement about ‘elsif v. elseif’.
I still think leaving the ‘e’ out is a ridiculous construct.

On 3/7/07, 7stud 7stud [email protected] wrote:

Another question. The tutorial says:

Save this file as “ri20min.rb”, and run it as “ruby ri20min.rb”.

and at the top of the file is the shebang:

#!/usr/bin/env ruby

If you run the program using ‘ruby filename’, do you need the shebang?
That is correct
I read something that said you only need the shebang if you want to
execute programs using just the filename, e.g.:

$ HelloWorld.rb
exactly

also what does ‘env’ do? I read the man pages on the env function, and
I can’t figure out what it does in the shebang.
It runs a program in a modified environment, it is frequently used in
the shebang because it is normally in a standard location while ruby
itself might be in
different locations e.g. /usr/bin or /usr/local/bin.
It is a trick to get this information from the “environment”.
But is is not always a good idea, try

#!/usr/bin/env ruby -w

it does not work -w is interpreted by env not by ruby :frowning:


Posted via http://www.ruby-forum.com/.

Robert

On Wed, Mar 07, 2007 at 08:08:35PM +0900, 7stud 7stud wrote:

with the ‘E’ in elseif in red.
Really, your complaint amounts to nothing more than “I’m more used to
the way language A does it than the way language B does it – so
language B must be wrong.”

Technically speaking, “elseif”, “elsif”, and “elif” are equally “wrong”.
To do it right, you’d have to make it “else if”.

7stud 7stud schrieb:

I still think leaving the ‘e’ out is a ridiculous construct.

It all depends where you come from. Other languages have had elsif
before Ruby:

Ada
Perl
PL/SQL
PostgreSQL

Just to name a few.

Regards,
Pit

On 3/7/07, Chad P. [email protected] wrote:

with the ‘E’ in elseif in red.

Really, your complaint amounts to nothing more than “I’m more used to
the way language A does it than the way language B does it – so
language B must be wrong.”

Technically speaking, “elseif”, “elsif”, and “elif” are equally “wrong”.
To do it right, you’d have to make it “else if”.
Chad you remind me of a dispute between fans of Domingo and Pavarotti
when
a spanish music magazin explained that such discussions are futile and
nobody can judge at that level. This explaination took a whole article
just in concluding that Carreras was better than both…

So why would “else if” be better?

Cheers
Robert

David A. Black wrote:

Hi –

On 3/7/07, 7stud 7stud [email protected] wrote:

Does Ruby try differentiate itself in ridiculous ways like that just for
the sake of being different?

No – literally never.

David

Yeah. Like the use of “throw” and “catch”; “.inject”; “yieieild” that’s
not really the yield of coroutines, but just a way to call the unnamed
closure (or the unnamed unnamed function) that was passed to the method
as the last parameter; the fact that “and” and “or” have the same
precedence, while && has bigger precedence than ||, etc. etc. etc.

The fact that I’m supposed to write

puts 1 + 2 +
3

or

puts 1 + 2 \

  • 3

(Visual Basic anyone?) it fairly … wuby as well.

Chad P. wrote:

On Wed, Mar 07, 2007 at 08:08:35PM +0900, 7stud 7stud wrote:

with the ‘E’ in elseif in red.
Really, your complaint amounts to nothing more than “I’m more used to
the way language A does it than the way language B does it – so
language B must be wrong.”

Technically speaking, “elseif”, “elsif”, and “elif” are equally “wrong”.
To do it right, you’d have to make it “else if”.

Yes, I guess you’re right. I’ve never seen ‘elsif’ or ‘elif’ before.
But couldn’t/shouldn’t that be expected? So why not point that out in
“Ruby in 20 Minutes”? There isn’t even anything about that in the
“Ruby from C and C++” page either, although instead of burying it in
there, I suggest it be deployed to the front lines.

Also, to be consistent, shouldn’t it be:

z = if x < y
true
els
false
end

On 3/7/07, 7stud 7stud [email protected] wrote:

Also, to be consistent, shouldn’t it be:

z = if x < y
true
els
false
end

Note the difference between

z = if x < y
-1
else if x == y
0
else if x < y
1
end
end
end

and

z = if x < y
-1
elsif x == y
0
elsif x < y
1
end

martin

Hi –

On 3/7/07, 7stud 7stud [email protected] wrote:

Does Ruby try differentiate itself in ridiculous ways like that just for
the sake of being different?

No – literally never.

David

7stud 7stud wrote:

How do you turn on syntax highlighting with vim?

:syntax on

I strongly advice you to use a syntax highlighting ediotr like e.g.
vim, emacs, Jedit, geany and tons of others.

How do you turn on syntax highlighting with vim?

IM - Vi IMproved
~
~ version 6.2

On 3/7/07, Martin DeMello [email protected] wrote:

and

Thank you Martin, I started to feel lonely :wink:
R

For the record, I find a few of Ruby’s naming choices silly and
non-intuitive as well. “elsif”, regardless of it’s language lineage,
IS kinda weird and easy to miss. “case” should have been “switch”.
And almost any of the proposed alternatives to “inject” would be
preferable - with my personal preferance being “fold”.

But I’m so used to dealing with language eccentricities, and Ruby’s
features give me so much joy, that it’s easy to overlook such
nitpicks.

Sebastian H. wrote:

7stud 7stud wrote:

How do you turn on syntax highlighting with vim?

:syntax on

Hey, I can see the elseif error now. :slight_smile:

No.
Programming languages are, like all languages, arbitrary symbolic
sets based on some sort of logical meaning.
In this case, someone else’s (els’) logic.
Like human languages, it does little good to complain about idioms or
grammar, just use it.
Life is much easier then. Every programming language has differences,
sometimes small subtle ones. The small subtle things are what make C
and C++ difficult to debug. This is why we have tools like colored
text editors and lexical analyzers and debuggers. Arguably, a
computer language should be more like a human language, but that too
is a bad idea. Human language is very implicit, contextual, and
fuzzy. When you are really dealing with 1s and 0s you can’t be so fuzzy.

7stud 7stud wrote:

explicitly pointed out in a beginning tutorial? So far, I have to give
Ruby two thumbs down.

C++, Java, Javascript, php, Servlets+JSP programmer

I have a similar language background (minus php). When I first looked at
Ruby, I spent a few minutes flipping through the Pickaxe, spotted what I
felt were some “Perlisms”, and made a knee jerk reaction to reject the
language. I’ve since talked to a few folks who did the same, so mine was
not an isolated incident.

It was about a year later that I came back to Ruby (because of Rails)
and discovered that I like the language (a lot). I’m sharing this
because learning from other people’s experience can be helpful. I don’t
know if you’ll end up enjoying the language as much as I have, but it
may be worthwhile to invest some more time with it before deciding. I
even realized that I actually like a few of the “Perlisms” - the horror!
:slight_smile:

“Chad P.” [email protected] wrote in message
news:[email protected]

“Different” would be more like the way bash does it: “elif”

Different?

Algol 68 spelled it that way in 1967, a spelling that the Bourne Shell
adopted in Unix in 1977.