Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #2232 > unrolled thread
| Started by | wolf volpi <wolf_volpi@yahoo.com> |
|---|---|
| First post | 2011-04-03 21:58 -0500 |
| Last post | 2011-04-03 23:55 -0500 |
| Articles | 13 — 7 participants |
Back to article view | Back to comp.lang.ruby
pipe question wolf volpi <wolf_volpi@yahoo.com> - 2011-04-03 21:58 -0500
Re: pipe question John Feminella <johnf@bitsbuilder.com> - 2011-04-03 22:08 -0500
Re: pipe question Robert Klemme <shortcutter@googlemail.com> - 2011-04-04 02:36 -0500
Re: pipe question Josh Cheek <josh.cheek@gmail.com> - 2011-04-03 22:42 -0500
Re: pipe question Josh Cheek <josh.cheek@gmail.com> - 2011-04-04 04:58 -0500
Re: pipe question Brian Candler <b.candler@pobox.com> - 2011-04-05 04:01 -0500
Re: pipe question 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-04 19:12 -0500
Re: pipe question Josh Cheek <josh.cheek@gmail.com> - 2011-04-04 22:30 -0500
Re: pipe question Robert Klemme <shortcutter@googlemail.com> - 2011-04-05 10:27 -0500
Re: pipe question 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-05 21:06 -0500
Re: pipe question Jesús Gabriel y Galán <jgabrielygalan@gmail.com> - 2011-04-06 02:00 -0500
Re: pipe question Josh Cheek <josh.cheek@gmail.com> - 2011-04-05 21:23 -0500
Re: pipe question wolf volpi <wolf_volpi@yahoo.com> - 2011-04-03 23:55 -0500
| From | wolf volpi <wolf_volpi@yahoo.com> |
|---|---|
| Date | 2011-04-03 21:58 -0500 |
| Subject | pipe question |
| Message-ID | <6351ecc5d2ac52f4b4c9d06ad2930f2c@ruby-forum.com> |
What does the pipe in this example do? @first_name = @first_name || '' According to the text book that this example came from, the code ensures that an instance variable is not nil. If @first_ name is nil, @first_ name is set to the empty string. Thank you. -- Posted via http://www.ruby-forum.com/.
[toc] | [next] | [standalone]
| From | John Feminella <johnf@bitsbuilder.com> |
|---|---|
| Date | 2011-04-03 22:08 -0500 |
| Message-ID | <BANLkTi=cZ6MQpz5Z-A7ZpSFJ6yeZ_nNZqA@mail.gmail.com> |
| In reply to | #2232 |
An expression like "foo || bar" works like this: First, evaluate the
boolean value of `foo`. If it is boolean-true, then the value of this
expression is `foo`. If not, then the value of this expression is
`bar`.
This expression says, assign the current value of @first_name back
into @first_name. Alternatively, if this is the first time you're
trying to assign it, it'll be nil, so use an empty string instead.
It's equivalent to writing:
@first_name ||= ''
~ jf
--
John Feminella
Principal Consultant, BitsBuilder
LI: http://www.linkedin.com/in/johnxf
SO: http://stackoverflow.com/users/75170/
On Sun, Apr 3, 2011 at 22:58, wolf volpi <wolf_volpi@yahoo.com> wrote:
> What does the pipe in this example do?
>
> @first_name = @first_name || ''
>
> According to the text book that this example came from, the code ensures
> that an instance variable is not nil. If @first_ name is nil, @first_
> name is set to the empty string.
>
> Thank you.
>
> --
> Posted via http://www.ruby-forum.com/.
>
>
[toc] | [prev] | [next] | [standalone]
| From | Robert Klemme <shortcutter@googlemail.com> |
|---|---|
| Date | 2011-04-04 02:36 -0500 |
| Message-ID | <BANLkTincGck23pt9q7YPMKTgrhYj+DADCg@mail.gmail.com> |
| In reply to | #2233 |
On Mon, Apr 4, 2011 at 5:08 AM, John Feminella <johnf@bitsbuilder.com> wrote: > An expression like "foo || bar" works like this: First, evaluate the > boolean value of `foo`. If it is boolean-true, then the value of this > expression is `foo`. If not, then the value of this expression is > `bar`. > > This expression says, assign the current value of @first_name back > into @first_name. Alternatively, if this is the first time you're > trying to assign it, it'll be nil, so use an empty string instead. > > It's equivalent to writing: > > @first_name ||= '' No. That is equivalent to: @first_name || @first_name = "" which is not the same as @first_name = @first_name || "" See also irb(main):004:0> x=Object.new => #<Object:0x10924b1c> irb(main):005:0> def x.[](k) puts "get";@x;end => nil irb(main):006:0> def x.[]=(k,v) puts "put";@x=v;end => nil irb(main):007:0> x[1] ||= 2 get put => 2 irb(main):008:0> x[1] ||= 2 get => 2 This has been discussed at lengths here in the past. You should be able to find plenty of evidence. :-) Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/
[toc] | [prev] | [next] | [standalone]
| From | Josh Cheek <josh.cheek@gmail.com> |
|---|---|
| Date | 2011-04-03 22:42 -0500 |
| Message-ID | <BANLkTimcgE-BpQ22=knOoRM_78KfFD2-rQ@mail.gmail.com> |
| In reply to | #2232 |
[Note: parts of this message were removed to make it a legal post.] On Sun, Apr 3, 2011 at 9:58 PM, wolf volpi <wolf_volpi@yahoo.com> wrote: > What does the pipe in this example do? > > @first_name = @first_name || '' > > According to the text book that this example came from, the code ensures > that an instance variable is not nil. If @first_ name is nil, @first_ > name is set to the empty string. > > Thank you. > > -- > Posted via http://www.ruby-forum.com/. > > 1) In Ruby, false and nil are treated as falsy. Everything else is treated as truthy. "truthy" if nil # => nil "truthy" if false # => nil "truthy" if true # => "truthy" "truthy" if "str" # => "truthy" "truthy" if 1 # => "truthy" "truthy" if 1.23 # => "truthy" "truthy" if /regex/ # => nil "truthy" if :symbol # => "truthy" "truthy" if 'r'..'ange' # => "truthy" 2) Boolean operators return the objects themselves. In an "or" (double pipes), if the first value is falsy, the second value is returned. If the first value is truthy, it is returned. nil || "b" # => "b" false || "b" # => "b" "a" || nil # => "a" "a" || false # => "a" "a" || "b" # => "a" nil || nil # => nil nil || false # => false false || nil # => nil false || false # => false 3) For some reason that I don't know (probably interpreter magic) an uninitialized variable can be referenced in a boolean equation and it will evaluate to nil. first_name = first_name || "" # => "" first_name = "josh" first_name = first_name || "" # => "josh" So, it will set the variable to the empty string, if the variable is undeclared, or false , or nil.
[toc] | [prev] | [next] | [standalone]
| From | Josh Cheek <josh.cheek@gmail.com> |
|---|---|
| Date | 2011-04-04 04:58 -0500 |
| Message-ID | <BANLkTin0gjsx2YHVvYq_zVE9TcbysBvFxw@mail.gmail.com> |
| In reply to | #2235 |
[Note: parts of this message were removed to make it a legal post.] On Sun, Apr 3, 2011 at 10:42 PM, Josh Cheek <josh.cheek@gmail.com> wrote: > 1) In Ruby, false and nil are treated as falsy. Everything else is treated > as truthy. > > "truthy" if nil # => nil > "truthy" if false # => nil > "truthy" if true # => "truthy" > "truthy" if "str" # => "truthy" > "truthy" if 1 # => "truthy" > "truthy" if 1.23 # => "truthy" > "truthy" if /regex/ # => nil > "truthy" if :symbol # => "truthy" > "truthy" if 'r'..'ange' # => "truthy" > > I just listed out a bunch of examples and then ran them.... But I'm looking now, and realize that /regex/ evaluated as falsy! After some playing around, I have decided that it is based on Perl. If you put a regex in a variable, it treats it as a boolean "is it an object?" and behaves as I stated previously. But if you put it in a literal, it plays Perl and checks to see if it matches against $_ regex = /re(.)ex/ $_ # => nil # in literal, so checks if it matches $_ # (note that it warns: regex literal in condition) "truthy" if /re(.)ex/ # => nil # in var, so checks if it exists "truthy" if regex # => "truthy" # explicitly set $_, now literal matches $_ = "reGex" "truthy" if /re(.)ex/ # => "truthy" # when we look at capture group, we see the G we made $1 # => "G" # change the $_ to see if variable matches it $_ = "reXex" "truthy" if regex # => "truthy" # nope, it should be "X" if it matched. $1 # => "G" # just to double check $1 if /re(.)ex/ # => "X" So, regex aren't nil or false, and are thus truthy. But when you throw a regex literal in the conditional, it does some implicit operations and doesn't pass the regex straight on through to the conditional.
[toc] | [prev] | [next] | [standalone]
| From | Brian Candler <b.candler@pobox.com> |
|---|---|
| Date | 2011-04-05 04:01 -0500 |
| Message-ID | <0c7c2a2161e99359a724f8a844f51895@ruby-forum.com> |
| In reply to | #2251 |
Josh Cheek wrote in post #990806: > If you put a regex in a > variable, it treats it as a boolean "is it an object?" and behaves as I > stated previously. But if you put it in a literal, it plays Perl and > checks > to see if it matches against $_ Yes, this is a very ugly inheritance from Perl. Check out the flip-flop operator too :-) -- Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | 7stud -- <bbxx789_05ss@yahoo.com> |
|---|---|
| Date | 2011-04-04 19:12 -0500 |
| Message-ID | <b6f24ad6461fa5fa75f8c1fe78d9f727@ruby-forum.com> |
| In reply to | #2235 |
Josh Cheek wrote in post #990762:
>
> 3) For some reason that I don't know (probably interpreter magic) an
> uninitialized variable can be referenced in a boolean equation and it
> will
> evaluate to nil.
>
> first_name = first_name || "" # => ""
> first_name = "josh"
> first_name = first_name || "" # => "josh"
>
>
> So, it will set the variable to the empty string, if the variable is
> undeclared, or false , or nil.
I think the story goes something like this: when the parser sees any
'name =' expression, name gets entered into the symbol table.
Thereafter, you will no longer get an exception when referencing the
variable. In your code, the parser sees 'first_name =', so first_name
is entered into the symbol table, and then when ruby executes your code,
the expression on the right hand side of the equals sign is executed.
Here is an example that might prove illustrative:
if 1 > 10
x = 'hello' #never executes
end
puts x #=> nil
puts y #=> undefined local variable or method `y'
#for main:Object (NameError)
--
Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | Josh Cheek <josh.cheek@gmail.com> |
|---|---|
| Date | 2011-04-04 22:30 -0500 |
| Message-ID | <BANLkTi=+_=4AF=+7ZM2YOiNWUa_SUqOaJg@mail.gmail.com> |
| In reply to | #2286 |
[Note: parts of this message were removed to make it a legal post.] On Mon, Apr 4, 2011 at 7:12 PM, 7stud -- <bbxx789_05ss@yahoo.com> wrote: > Josh Cheek wrote in post #990762: > > > > 3) For some reason that I don't know (probably interpreter magic) an > > uninitialized variable can be referenced in a boolean equation and it > > will > > evaluate to nil. > > > > first_name = first_name || "" # => "" > > first_name = "josh" > > first_name = first_name || "" # => "josh" > > > > > > So, it will set the variable to the empty string, if the variable is > > undeclared, or false , or nil. > > I think the story goes something like this: when the parser sees any > 'name =' expression, name gets entered into the symbol table. > Thereafter, you will no longer get an exception when referencing the > variable. In your code, the parser sees 'first_name =', so first_name > is entered into the symbol table, and then when ruby executes your code, > the expression on the right hand side of the equals sign is executed. > > Makes sense, and passed the test I constructed to invalidate it: lhs = rhs || "pass" lhs # => # ~> -:1:in `<main>': undefined local variable or method `rhs' for main:Object (NameError) I probably never figured that out because it depends on the variable type, for example: lhs = @rhs || "pass" # => "pass" and lhs = $rhs || "pass" # => "pass" > Here is an example that might prove illustrative: > > if 1 > 10 > x = 'hello' #never executes > end > > puts x #=> nil > puts y #=> undefined local variable or method `y' > #for main:Object (NameError) > > Good thought. I'm honestly surprised that works! I guess it must add x to the symbol table at parse time.
[toc] | [prev] | [next] | [standalone]
| From | Robert Klemme <shortcutter@googlemail.com> |
|---|---|
| Date | 2011-04-05 10:27 -0500 |
| Message-ID | <BANLkTi=i2-vkT1M5WWjXMa8y-hHOUkFO9g@mail.gmail.com> |
| In reply to | #2290 |
On Tue, Apr 5, 2011 at 4:35 PM, Chad Perrin <code@apotheon.net> wrote: > On Tue, Apr 05, 2011 at 12:30:47PM +0900, Josh Cheek wrote: >> On Mon, Apr 4, 2011 at 7:12 PM, 7stud -- <bbxx789_05ss@yahoo.com> >> wrote: >> > >> > Here is an example that might prove illustrative: >> > >> > if 1 > 10 >> > x = 'hello' #never executes >> > end >> > >> > puts x #=> nil >> > puts y #=> undefined local variable or method `y' >> > #for main:Object (NameError) >> >> Good thought. I'm honestly surprised that works! I guess it must add x >> to the symbol table at parse time. > > Is that something we should be able to depend on, though -- or just an > accident of implementation? I think it's a language feature. From the line where an assignment to a identifier is found this identifier denotes a local variable - whether the code is actually executed does not matter. This is only about syntactical order. Consider $ ruby19 -e 'puts(x) if x=1' -e:1: warning: found = in conditional, should be == -e:1:in `<main>': undefined local variable or method `x' for main:Object (NameError) $ ruby19 -e 'x=1;puts(x) if x' 1 $ Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/
[toc] | [prev] | [next] | [standalone]
| From | 7stud -- <bbxx789_05ss@yahoo.com> |
|---|---|
| Date | 2011-04-05 21:06 -0500 |
| Message-ID | <055d7420f4512beb655b878560afb345@ruby-forum.com> |
| In reply to | #2290 |
Josh Cheek wrote in post #990917: > > I probably never figured that out because it depends on the variable > type, > for example: > lhs = @rhs || "pass" # => "pass" > and > lhs = $rhs || "pass" # => "pass" Remember, by rule instance variables and global variables have a default value of nil. -- Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | Jesús Gabriel y Galán <jgabrielygalan@gmail.com> |
|---|---|
| Date | 2011-04-06 02:00 -0500 |
| Message-ID | <BANLkTimZN2JEqh79NXsLj3XLgo8jJ0X4hg@mail.gmail.com> |
| In reply to | #2290 |
On Tue, Apr 5, 2011 at 4:35 PM, Chad Perrin <code@apotheon.net> wrote: > On Tue, Apr 05, 2011 at 12:30:47PM +0900, Josh Cheek wrote: >> On Mon, Apr 4, 2011 at 7:12 PM, 7stud -- <bbxx789_05ss@yahoo.com> >> wrote: >> > >> > Here is an example that might prove illustrative: >> > >> > if 1 > 10 >> > x = 'hello' #never executes >> > end >> > >> > puts x #=> nil >> > puts y #=> undefined local variable or method `y' >> > #for main:Object (NameError) >> >> Good thought. I'm honestly surprised that works! I guess it must add x >> to the symbol table at parse time. > > Is that something we should be able to depend on, though -- or just an > accident of implementation? I think it's a language feature. In the Ruby Draft Specification (http://ruby-std.netlab.jp/draft_spec/draft_ruby_spec-20091201-hl.pdf) you can read this: 9.1.2 References to local variables An occurrence of a local-variable-identifier can be a reference to a local variable or a method invocation. In order to determine whether the occurrence of a local-variable-identifier is a reference to a local variable or a method invocation, before the evaluation of a local variable scope, the scope is scanned sequentially for local-variable-identifier s. For each occurrence of a local-variable-identifier I, take the following steps: a) If I occurs in one of the forms below, I is a reference to a local variable. 1 ˆ mandatory-parameter 2 ˆ optional-parameter-name 3 ˆ array-parameter-name 4 ˆ block-parameter-name 5 ˆ variable of left-hand-side 6 ˆ variable of single-variable-assignment-expression 7 ˆ variable of single-variable-assignment-statement 8 ˆ variable of abbreviated-variable-assignment-expression 9 ˆ variable of abbreviated-variable-assignment-statement As you can see, number 5 (variable of LHS) covers the case we are discussing here. Jesus.
[toc] | [prev] | [next] | [standalone]
| From | Josh Cheek <josh.cheek@gmail.com> |
|---|---|
| Date | 2011-04-05 21:23 -0500 |
| Message-ID | <BANLkTinRmg7=DJ-xeQBPxd-0TsrcQVjjVQ@mail.gmail.com> |
| In reply to | #2286 |
[Note: parts of this message were removed to make it a legal post.] On Mon, Apr 4, 2011 at 7:12 PM, 7stud -- <bbxx789_05ss@yahoo.com> wrote: > Josh Cheek wrote in post #990762: > > > > 3) For some reason that I don't know (probably interpreter magic) an > > uninitialized variable can be referenced in a boolean equation and it > > will > > evaluate to nil. > > > > first_name = first_name || "" # => "" > > first_name = "josh" > > first_name = first_name || "" # => "josh" > > > > > > So, it will set the variable to the empty string, if the variable is > > undeclared, or false , or nil. > > I think the story goes something like this: when the parser sees any > 'name =' expression, name gets entered into the symbol table. > Thereafter, you will no longer get an exception when referencing the > variable. In your code, the parser sees 'first_name =', so first_name > is entered into the symbol table, and then when ruby executes your code, > the expression on the right hand side of the equals sign is executed. > > Actually, I was thinking about it, and this contradicts that a||=b expands to a||a=b undefined1 ||= :defined undefined1 # => :defined undefined2 = ( undefined2 || :defined ) undefined2 # => :defined undefined3 || ( undefined3 = :defined ) undefined3 # => # ~> -:7:in `<main>': undefined local variable or method `undefined3' for main:Object (NameError)
[toc] | [prev] | [next] | [standalone]
| From | wolf volpi <wolf_volpi@yahoo.com> |
|---|---|
| Date | 2011-04-03 23:55 -0500 |
| Message-ID | <69adf7ea6df8d60dfc1db2c7d6fdd29c@ruby-forum.com> |
| In reply to | #2232 |
Ha Ha. I was interpeting the pipes as empty arguments of an empty block, which made no sence to me. Logical operators didn't even occure to me. Seems funny now. Thank you John and Josh for helping me see the light. The || is a Logical OR Operator that returns the first true value (in Ruby anything besides false and nil is true). I ran this demonstration in irb: >> true || true => true >> true || false => true >> false || true => true >> false || false => false >> 1 || 2 => 1 >> 1 || false => 1 >> false || 2 => 2 >> false || false => false >> 1 || 2 => 1 >> 1 || nil => 1 >> nil || 2 => 2 >> nil || nil => nil >> nil || nil || 3 || 4 => 3 -- Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.ruby
csiph-web