Ruby消息给自己

时间:2017-07-15 07:53:34

标签: ruby

如果我运行以下Ruby代码:

    viewPager = (ViewPager) view.findViewById(R.id.view_pager);
    mAdapter = new PromoPagerAdapter(getActivity());
    viewPager.setAdapter(mAdapter);
    viewPager.addOnPageChangeListener(this);
    CirclePageIndicator circlePageIndicator = (CirclePageIndicator)      view.findViewById(R.id.page_indicator);
    circlePageIndicator.setViewPager(viewPager);



@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override
public void onPageSelected(int position) {

    mCurrentPosition = position;
}

@Override
public void onPageScrollStateChanged(int state) {

    int currentPage = viewPager.getCurrentItem();
    if (currentPage == mAdapter.getCount()-1 || currentPage == 0) {
        previousState = mCurrentPosition;
        mCurrentPosition = state;
        if (previousState == 1 && mCurrentPosition == 0) {
            viewPager.setCurrentItem(currentPage == 0 ? mAdapter.getCount()-1 : 0);
        }
    }
}

输出为:class Foo def foo=(something) puts "It's a #{something}" end def foo_assign self.foo = "bar" end end f = Foo.new f.foo_assign

另一方面,如果我运行代码:

It's a bar

没有输出。如何class Foo def foo=(something) puts "It's a #{something}" end def foo_assign foo = "bar" end end f = Foo.new f.foo_assign foo等同?

1 个答案:

答案 0 :(得分:1)

foo = "bar"foo_assign方法中含糊不清。 Ruby认为你试图分配一个局部变量而不是调用方法,这样它就能为你做什么。 (想象一下在下一行设置bar = "foo",它仍然可以工作,即使没有bar=方法)。如果您尝试做或打电话不明确,则必须使用self.。比我更好的人可以更好或更清楚地解释这一点,但这就是这里发生的事情。