Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > elseif v. elsif ??

Reply
Thread Tools

elseif v. elsif ??

 
 
7stud 7stud
Guest
Posts: n/a
 
      03-07-2007
What the?? I just spent two days trying to figure out why I couldn't
reproduce the example in "Ruby in 20 minutes" and get it to work. After
examining my code line for line against the example code and not being
able to detect any error, I was assembling several code examples into a
text file to post here, when I happened to notice 'elsif'. Why
didn't Ruby flag 'elseif' as an error?


Does Ruby try differentiate itself in ridiculous ways like that just for
the sake of being different? And why isn't something like that
explicitly pointed out in a beginning tutorial? So far, I have to give
Ruby two thumbs down.

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

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

 
Reply With Quote
 
 
 
 
Chad Perrin
Guest
Posts: n/a
 
      03-07-2007
On Wed, Mar 07, 2007 at 05:56:56PM +0900, 7stud 7stud wrote:
>
> Does Ruby try differentiate itself in ridiculous ways like that just for
> the sake of being different? And why isn't something like that
> explicitly pointed out in a beginning tutorial? So far, I have to give
> Ruby two thumbs down.


Ruby isn't the only language that does that.

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

--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
"There comes a time in the history of any project when it becomes necessary
to shoot the engineers and begin production." - MacUser, November 1990

 
Reply With Quote
 
 
 
 
Robert Dober
Guest
Posts: n/a
 
      03-07-2007
On 3/7/07, 7stud 7stud <(E-Mail Removed)> wrote:
> What the?? I just spent two days trying to figure out why I couldn't
> reproduce the example in "Ruby in 20 minutes" and get it to work. After
> examining my code line for line against the example code and not being
> able to detect any error, I was assembling several code examples into a
> text file to post here, when I happened to notice 'elsif'. Why
> didn't Ruby flag 'elseif' as an error?

Because it nvere sees it

Look at two examples

if true then
whatever
elseif
end

now elsif is seen as an undefined method but

if false then
whatever
elseif
end

whatever and elseif are not evaluated.

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

Cheers
Robert
>
>
> Does Ruby try differentiate itself in ridiculous ways like that just for
> the sake of being different? And why isn't something like that
> explicitly pointed out in a beginning tutorial? So far, I have to give
> Ruby two thumbs down.
>
> C++, Java, Javascript, php, Servlets+JSP programmer
>
> --
> Posted via http://www.ruby-forum.com/.
>
>



--
We have not succeeded in answering all of our questions.
In fact, in some ways, we are more confused than ever.
But we feel we are confused on a higher level and about more important things.
-Anonymous

 
Reply With Quote
 
Hans Sjunnesson
Guest
Posts: n/a
 
      03-07-2007
On Mar 7, 9:56 am, 7stud 7stud <(E-Mail Removed)> wrote:
> What the?? I just spent two days trying to figure out why I couldn't
> reproduce the example in "Ruby in 20 minutes" and get it to work. After
> examining my code line for line against the example code and not being
> able to detect any error, I was assembling several code examples into a
> text file to post here, when I happened to notice 'elsif'. Why
> didn't Ruby flag 'elseif' as an error?
>
> Does Ruby try differentiate itself in ridiculous ways like that just for
> the sake of being different? And why isn't something like that
> explicitly pointed out in a beginning tutorial? So far, I have to give
> Ruby two thumbs down.
>
> C++, Java, Javascript, php, Servlets+JSP programmer
>
> --
> Posted viahttp://www.ruby-forum.com/.


Well, Ruby doesn't try to differentiate itself in ridiculous ways just
for the sake of being different. It's not a person.
However, it will spit out a "undefined method 'elseif' for main:Object
(NoMethodError)" when you use 'elseif', so it's really not a problem
is it?

--
Hans

 
Reply With Quote
 
Stefano Crocco
Guest
Posts: n/a
 
      03-07-2007
Alle mercoled=EC 7 marzo 2007, Robert Dober ha scritto:
> if true then
> =A0 =A0whatever
> =A0 =A0elseif
> end
>
> now elsif is seen as an undefined method but


Not always. In Robert's first example,=20

> if true then
> whatever
> elseif
> end


you'll get a NameError (undefined local variable or method `elseif' for=20
main:Object (NameError))

In the following example, instead, you get a syntax error:

if x < 0 then puts "x<0"
elseif x < 3 then puts "0<=3Dx<3"
else puts "x>=3D3"
end

The error message is:

syntax error, unexpected kTHEN, expecting kEND
elseif x < 3 then puts "0<=3Dx<3"
^
Here, ruby doesn't complain because elseif doesn't exist, but because it fi=
nds=20
a 'then' where it shouldn't be (not following an if or elsif clause). By th=
e=20
way, being a syntax error (it when the interpreter is parsing the file, not=
=20
when it executes it), this error is reported whatever the value of x is (an=
d=20
even if x doesn't exist).

All these error messages aren't very easy to understand for a novice. To ma=
ke=20
a comparison with other programming languages, I tried compiling a C progra=
m=20
with a similar mistake (in this case writing 'elseif' instead of 'else if')=
=2E=20
The program was:

int main(){
int a=3D3;
int b=3D0;
if( a=3D=3D4){ b=3D1;}
elseif(a=3D=3D2){ b=3D2;} //should be else if
else{ b=3D3;}}
}

Compiling with gcc, the error message I got is:

test.c: In function 'main':
test.c:5: error: expected ';' before '{' token

As you can see, the error message doesn't speak of invalid keywords, but j=
ust=20
of a missing ;

Stefano

 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      03-07-2007
On 3/7/07, Hans Sjunnesson <(E-Mail Removed)> wrote:
> On Mar 7, 9:56 am, 7stud 7stud <(E-Mail Removed)> wrote:
> > What the?? I just spent two days trying to figure out why I couldn't
> > reproduce the example in "Ruby in 20 minutes" and get it to work. After
> > examining my code line for line against the example code and not being
> > able to detect any error, I was assembling several code examples into a
> > text file to post here, when I happened to notice 'elsif'. Why
> > didn't Ruby flag 'elseif' as an error?
> >
> > Does Ruby try differentiate itself in ridiculous ways like that just for
> > the sake of being different? And why isn't something like that
> > explicitly pointed out in a beginning tutorial? So far, I have to give
> > Ruby two thumbs down.
> >
> > C++, Java, Javascript, php, Servlets+JSP programmer
> >
> > --
> > Posted viahttp://www.ruby-forum.com/.

>
> Well, Ruby doesn't try to differentiate itself in ridiculous ways just
> for the sake of being different. It's not a person.
> However, it will spit out a "undefined method 'elseif' for main:Object
> (NoMethodError)" when you use 'elseif', so it's really not a problem
> is it?
>
> --
> Hans
>
>
>

No of course it is not
I think to understand the frustration of OP however.
He is probably coming from a completely different world and it is not
always easy to grasp new concepts.
Therefore I preferred to ignore the aggressive nature of the post .
He might even have a point when he says that this is maybe not really
well documented, With this I do not mean the "elseif" of course but
just the dynamic evaluation of the code.

Maybe a chapter for that kind of pitfalls could be added somewhere -
well it probably is already, maybe somebody can indicate that.

This is however not a clearcut thing as it might seem at first view.

Robert


--
We have not succeeded in answering all of our questions.
In fact, in some ways, we are more confused than ever.
But we feel we are confused on a higher level and about more important things.
-Anonymous

 
Reply With Quote
 
Chris Lowis
Guest
Posts: n/a
 
      03-07-2007
> Maybe a chapter for that kind of pitfalls could be added somewhere -
> well it probably is already, maybe somebody can indicate that.


Perhaps at the "Ruby from other languages" page :
http://www.ruby-lang.org/en/document...her-languages/

I find this page very helpful.

Regards,


Chris

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

 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      03-07-2007
On 3/7/07, Stefano Crocco <(E-Mail Removed)> wrote:
<snip>

Good points Stefano, conclusion *always* use "then"
--
We have not succeeded in answering all of our questions.
In fact, in some ways, we are more confused than ever.
But we feel we are confused on a higher level and about more important things.
-Anonymous

 
Reply With Quote
 
7stud 7stud
Guest
Posts: n/a
 
      03-07-2007
Chris Lowis wrote:
>> Maybe a chapter for that kind of pitfalls could be added somewhere -
>> well it probably is already, maybe somebody can indicate that.

>
> Perhaps at the "Ruby from other languages" page :
> http://www.ruby-lang.org/en/document...her-languages/
>
> I find this page very helpful.
>
> Regards,
>
>
> Chris



First, I'd like to say that the web site is really beautiful and eye
catching. There are some minor problems, for instance, the code
examples are in a small area width wise, so there is a horizontal scroll
bar that you need to scroll to the right to see the latter portion of a
line of code. However, the area with the code is very tall(more than
one screen), and it is very inconvenient to page all the way down to the
bottom in order to scroll to the right, and then go all the way back up
in order to read the code. Also, no matter how wide I make my browser
window(Safari 2.0.4), the area with the code does not expand
horizontally. It should expand horizontally as the browser window gets
wider, and the horizontal scroll bars should disappear.

If I run the following code, I don't get any errors:

class MegaGreeter
attr_accessor :names

#constructor
def initialize(names = "world")
@names = names
end

#functions:
def say_hi
if @names.nil?
puts "..."
elseif @names.respond_to?("each")
@names.each do |name|
puts "Hello #{name}!"
end
else
puts "Hello #{@names}!"
end
end
end
if __FILE__ == $0
mg = MegaGreeter.new(["Sally", "Jane", "Bob"])
mg.say_hi
end


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

 
Reply With Quote
 
7stud 7stud
Guest
Posts: n/a
 
      03-07-2007
My output is:

~/2testing/dir1$ ruby rubyHelloWorld.rb
Hello SallyJaneBob!

ruby version:

~/2testing/dir1$ ruby -v
ruby 1.8.2 (2004-12-25) [universal-darwin8.0]

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Does the last elseif run or not? fl VHDL 6 02-28-2013 10:28 AM
Newbie: if / elseif planetthoughtful Ruby 10 03-16-2007 11:05 PM
Avoiding if..elsif statements unexpected Python 11 08-27-2006 12:58 PM
elseif problem simonced Ruby 7 06-03-2006 07:50 PM
ElseIF or Case Statement? Leon ASP .Net 5 12-17-2004 05:33 PM



Advertisments