Exclusive operator in ruby

Hi
I am C++ programmer and I start ruby programming about 1 month.I have 2
questions:
1 - I don't understand what is these operators : === and !== . please
give an example.
2 - As respect , Ruby is very comfort language , can we define function
like this :
def myFunc( a=0 , b=1 , c=2 )
d = a+b+c
end

and call this function with only third argument like :
myFunc( , , 10).

thanks

···

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

"amir e." <aef1370@gmail.com> writes:

Hi
I am C++ programmer and I start ruby programming about 1 month.I have 2
questions:
1 - I don't understand what is these operators : === and !== . please
give an example.

'Object#===' method is the same as 'Object#==', but is often overridden
by the descendants for convenience (mainly to provide meaningful
semantics in 'case' statements). For example, Regexp class makes it an
alias to its '=~' method, so you can do things like this:

a = "HELLO"
case a
when /^[a-z]*$/; :lower_case
when /^[A-Z]*$/; :upper_case
else; :mixed_case
end #=> :upper_case

(a !== b), as you might expect, is equivalent to !(a === b).

2 - As respect , Ruby is very comfort language , can we define function
like this :
def myFunc( a=0 , b=1 , c=2 )
d = a+b+c
end

and call this function with only third argument like :
myFunc( , , 10).

You can not do this (ruby does not support keyword arguments), but it is
possible to achieve similar result by passing your arguments as hash:

def my_func(hsh)
  hsh = {:a => 0, :b => 1, :c => 2}.merge hsh
  hsh[:a] + hsh[:b] + hsh[:c]
end

my_func({:c => 10}) #=> 11

2 - As respect , Ruby is very comfort language , can we define function
like this :
def myFunc( a=0 , b=1 , c=2 )
d = a+b+c
end

and call this function with only third argument like :
myFunc( , , 10).

You can if you're using 1.9. This isn't keyword arguments.

def x(a = 1, b = 2, c)
  [a, b, c]
end

=> nil

x(2)

=> [1, 2, 2]

x(3)

=> [1, 2, 3]

x(5, 6)

=> [5, 2, 6]

x(15, 20, 25)

=> [15, 20, 25]

···

On Sun, Jul 24, 2011 at 11:12 AM, amir e. <aef1370@gmail.com> wrote:

Are you sure about this one? I can't get it to work:

- ------------------------------------------------------
#ruby -v: ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-linux]
irb(main):001:0> 3 !== 4
SyntaxError: (irb):1: syntax error, unexpected '='
3 !== 4
     ^
  from /opt/rubies/ruby-1.9.2-p290/bin/irb:12:in `<main>'
- ------------------------------------------------------

AFAIK, there is no !== operator in Ruby. If so, then why doesn't the
above work?

Vale,
Marvin

···

Am 24.07.2011 13:30, schrieb Victor Deryagin:

(a !== b), as you might expect, is equivalent to !(a === b).

2 - As respect , Ruby is very comfort language , can we define function
like this :
def myFunc( a=0 , b=1 , c=2 )
d = a+b+c
end

and call this function with only third argument like :
myFunc( , , 10).

You can if you're using 1.9. This isn't keyword arguments.

Nice!

def x(a = 1, b = 2, c)
[a, b, c]
end

=> nil

x(2)

=> [1, 2, 2]

x(3)

=> [1, 2, 3]

x(5, 6)

=> [5, 2, 6]

x(15, 20, 25)

=> [15, 20, 25]

The variant with keyword arguments:

irb(main):030:0> def x(ag={})
irb(main):031:1> ag = {a:1,b:2,c:3}.merge ag
irb(main):032:1> [ag[:a], ag[:b], ag[:c]]
irb(main):033:1> end
=> nil
irb(main):034:0> x
=> [1, 2, 3]
irb(main):035:0> x(a:99)
=> [99, 2, 3]
irb(main):036:0> x(c:99)
=> [1, 2, 99]
irb(main):037:0> x(c:99,b:22)
=> [1, 22, 99]

Kind regards

robert

···

On Sun, Jul 24, 2011 at 5:57 PM, Adam Prescott <adam@aprescott.com> wrote:

On Sun, Jul 24, 2011 at 11:12 AM, amir e. <aef1370@gmail.com> wrote:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

AFAIK, there is no !== operator in Ruby. If so, then why doesn't the
above work?

Yes, you are right, there is no such operator. My mistake.