tags:

views:

237

answers:

7

I have a background from languages which use {} to say that these are "block of statements" but i am learning ruby and really confused how it being done there.

So lets say in C i have

if ( condition )
{
  statement1;
  statement2;
}
else if (condition)
{
  statement1;
  statement2;

// nested if
       if (condition)
       {
          dosomethinghere;
       }
    }
    else
    {
      statement1;
      statement2;
    }

How i can put this code in ruby? and also mention how to understand synomenous of "{}" in ruby, thanks.

+7  A: 

Well, to answer your first question:

if ( condition )
  statement1
  statement2
elsif (condition)
  statement1
  statement2
   // nested if
   if (condition)
     dosomethinghere
   end
else
  statement1
  statement2
end

The syntax rule for if statement is:

 if expr [then]
   expr...
[elsif expr [then]
    expr...]...
[else
   expr...]
 end

Where everything between [] is optional

However, and in other direction, you can also create and pass code blocks, check this post to read more about this topic.

khelll
So every "if" need an "end" but else block dont, right?
itsaboutcode
Have edited the post, check if syntax rule, and to answer your question, yes you are alright in that assumption.
khelll
Thanks man, i think after little bit of practice i will get much better idea, but it was really helpful.
itsaboutcode
+3  A: 

The ruby syntax for if is:

if condition body else alternativa body end

Or

if condition then body else alternative body end

It's the same for while loops except with do instead of then.

{ and } are used to pass anonymous functions (confusingly called "blocks" in ruby) as arguments to methods.

sepp2k
i have seen something like "begin end", what is that? can i say its same as {}?
itsaboutcode
begin end introduces a new variable scope, so in that sense it is like `{}` in C, yes. It is more commonly used with rescue, though, which makes it like try catch in C++.
sepp2k
begin end does _not_ introduce a new scope, unless that changed while I wasn't looking $ ruby -ve 'begin; x = 2; end; p x'ruby 1.8.6 (2007-06-07 patchlevel 36) [i486-linux]2
Logan Capaldo
@Logan: Huh. I could've sworn it did.
sepp2k
A: 

in Ruby, the opening brace is implied after an if. to close the block, you use an end instead of a close brace. the only other difference is you use elsif (condition) instead of else if (condition).

twolfe18
I have understood that, but if "if" has an "end" to indicate that its finished, then why "elsif" or "else" dont have an "end" to say that these are its block of statement?
itsaboutcode
ruby groups `if..elsif..else..end`'s into a single block, so you only need one `end` to close all of them.
twolfe18
A: 

If you're thinking "how do I create a new variable scope in Ruby"? ie:

{
    var myvar = 1;
}
myvar = 2; // compile error because myvar isn't in this scope!

I'm not really sure how you would do that.

RyanWilcox
`-> { myvar = 1 }.();``myvar #=> undefined local variable`
banister
+1  A: 

I'd suggest getting a decent book and sitting down and reading the first few chapters this should cover everything you asked here and a lot more. I'd suggest http://oreilly.com/catalog/9780596529864 although if you're trying to get something done really quick http://www.troubleshooters.com/codecorn/ruby/basictutorial.htm is quite a good brief intro to get you started.

Jamie
A: 

If you want a scope you can define your own scope method:

def scope
    yield
end

# use like this
scope {
    x = 5
    puts x #=> 5
}

x #=> undefined local variable

EDIT: for a better approach to 'scopes' in Ruby 1.9 see: http://banisterfiend.wordpress.com/2010/01/07/controlling-object-scope-in-ruby-1-9/

banister
NB this does not allow you to shadow a variable in an enclosing scope, `x = 2; scope { x = 5 ; puts x } ; puts x`, which can be surprising if someone comes along and adds a variable in the enclosing scope with the same name as one you're using the scope block.
Logan Capaldo
@logan, you are right. Here is a solution that does shadow vars in the enclosing scope (in 1.9): http://banisterfiend.wordpress.com/2010/01/07/controlling-object-scope-in-ruby-1-9/
banister
A: 

try running the following:

def example(x,y)

puts "X:#{x},Y:#{y}"

if ( x == 0 ) then
  puts "Its true"
elsif (x == 1)
  puts "Its not true"
  puts "it certainly isn't"
    if (y == 0) then
      puts "i'm the nested if"
    end
else
  puts "i made it to the default case"
  puts "freedom"
end
  puts 
end


example(0,0)
example(1,0)
example(1,1)
example(2,2)
Beanish